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”);
1 Like
I answered a similar problem before.
1 Like
Thanks so much! I set my initial test variable to true and got rid of the else and it passed!
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.
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;
}
Like @rmdawson71 said, your code doesn’t take into account numbers in the string.