I’m having trouble with the palindrome checker challenge. My function works fine for the first few test cases, but fails as soon as it encounters spaces, like “race car.” According to the challenge rules, the checker should ignore non-alphanumeric characters and convert everything to lowercase, which I’ve done, but for some reason the section of the code that removes non-alphanumeric characters doesn’t seem to work on spaces, despite the fact that spaces don’t meet /^[a-z0-9]/ and I’ve said to remove things that don’t meet those requirements. Any input with spaces in it is automatically labelled not a palindrome. What’s wrong here?
function palindrome(str) {
// Make an array
arr = str.split('');
// Make array all alphanumeric and lowercase
for (var i = 0; i < arr.length; i++) {
if (arr[i] != /^[a-z0-9]/) {
arr.splice(i, 1);
}
}
str = arr.join("");
str = str.toLowerCase();
// Test if palindrome
for (var j = 0; j < Math.floor((str.length + 1) / 2); j++) {
if (str[j] != str[str.length - 1 - j]) {
return false;
}
}
return true;
}