Complete the code to check if a string is a palindrome by reversing it.
function isPalindrome(str) {
const reversed = str.split('').reverse().join('');
return str === [1];
}The reversed string is stored in the variable reversed. To check if the original string is a palindrome, compare str with reversed.
Complete the code to check palindrome by comparing characters from start and end.
function isPalindrome(str) {
for (let i = 0; i < [1]; i++) {
if (str[i] !== str[str.length - 1 - i]) {
return false;
}
}
return true;
}We only need to check up to half the string length because we compare characters from both ends moving towards the center.
Fix the error in the code to correctly check palindrome ignoring case.
function isPalindrome(str) {
str = str.toLowerCase();
for (let i = 0; i < str.length / 2; i++) {
if (str[i] !== [1]) {
return false;
}
}
return true;
}To compare characters from both ends, use str[str.length - 1 - i] to get the matching character from the end.
Fill both blanks to create a function that removes non-alphanumeric characters and checks palindrome.
function isPalindrome(str) {
const cleaned = str.toLowerCase().replace(/[1]/g, '');
for (let i = 0; i < cleaned.length / 2; i++) {
if (cleaned[i] !== cleaned[[2]]) {
return false;
}
}
return true;
}The regex [^a-z0-9] removes all characters except letters and numbers. The index cleaned.length - 1 - i accesses the mirrored character from the end.
Fill all three blanks to create a palindrome check using array methods and filtering.
function isPalindrome(str) {
const filtered = str.toLowerCase().split('').filter(char => /[1]/.test(char));
const reversed = [...filtered].reverse();
return filtered.[2]((char, index) => char === reversed[[3]]);
}some instead of every causing incorrect results.The regex [a-z0-9] filters only letters and numbers. The every method checks if all characters match their reversed counterparts using the index.
