Palindrome("1 eye for of 1 eye.")

Tell us what’s happening:
Hi,

I’m a little confused about one of the tests for the palindrome algorithm. Why should palindrome(“1 eye for of 1 eye.”) be returning false? After removing all non-letter characters this sentence is a palindrome. My code returned true for this string. Am I missing something?

Thanks!

Your code so far

function palindrome(str) {
  var isPali = true;
  var newStr = str.toLowerCase().replace(/([^a-z])*/g, "").split("");
  for (var i=0; i<Math.floor(newStr.length/2); i++){
    if (newStr[i] !== newStr[newStr.length-1-i]) isPali = false;
  }
  return isPali;
}```
**Your browser information:**

Your Browser User Agent is: ```Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.181 Safari/537.36```.

**Link to the challenge:**
https://www.freecodecamp.org/challenges/check-for-palindromes
1 Like

alphanumeric, so [^a-z0-9]. If you don’t eliminate the numbers with your regex you’ll see that it is no longer a palindrome.

Also a tip: isPali variable is a bit of a waste, do you need to continue the loop when isPali is set to false?

1 Like

Ooh, guess I misread, thought that numbers should be eliminated as well. I also fixed the code so the function would return false as soon as it’s not a palindrome, so the loop doesn’t keep going. Thanks for your help!

1 Like