Check for Palindromes - 1 eye for of 1 eye help!

Check for Palindromes - 1 eye for of 1 eye help!
0.0 0

#1

Hi,

My code passes all the other tests except for ‘1 eye for of 1 eye’ - it should return false and it’s returning true.
Any suggestions? I’m pretty happy with the logic I’ve come up with, but must be missing something…

function palindrome(str) {
str = str.replace(/[^a-zA-Z0-9]/g,’’);
str = str.toLowerCase();
var test;
for (i = 0; i < (str.length/2); i++) {
if (str[i] !== str[(str.length -1 - i)]) {
test = false;
}
else {
test = true;
}

}
return test;

}

palindrome(“1 eye for of 1 eye”);


#2

I answered a similar problem before.


#3

Thanks so much! I set my initial test variable to true and got rid of the else and it passed!


#4

I still don’t get what you mean. I never had an else in my if-block and I am still getting that only input wrong.


#5

My code below.

function palindrome(str) {
// Good luck!
str = str.replace(/[^a-z]/gi, ‘’);
str = str.toLowerCase();
var arraySplitStr = str.split(’’);
var arrayReverseStr = arraySplitStr.reverse();
var reverseStr = arrayReverseStr.join(’’);
if(reverseStr !== str){
return false;
}
return true;
}


#6

@GodBlack001 - Why are you replacing numbers with blank string in the following line?

str = str.replace(/[^a-z]/gi, ‘’);

The instructions say remove all non-alphanumeric characters not non-alpha characters.


#7

Like @rmdawson71 said, your code doesn’t take into account numbers in the string.