Javascript: Palindrome checker help

Hi, I have all cases passing except one and I can’t figure out a way to include it in my regex, I’ve played around to no avail so far. Any guidance would be appreciated.

Test case not passing is the one in the argument to the function at the bottom of the code.

Link: https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/palindrome-checker

Expected output is false but at the moment is returning true:

// running tests
palindrome("1 eye for of 1 eye.") should return false.
// tests completed
// console output

This is because the regex I am using is removing the 1 to compare, but I’m unsure how to include it in the regex to make it pass this case without affecting the others.

function palindrome(str) {
  let regex = /[^A-Za-z]/g
  let check = str
  .replace(regex, "")
  .toLowerCase();

  let newStr = str
  .replace(regex, "")
  .toLowerCase()
  .split("")
  .reverse()
  .join("");

  console.log(check);
  console.log(newStr);

  if(newStr === check) {
    return true;
  } else {
    return false;
  }
  
}

palindrome("1 eye for of 1 eye.");

what’s the expected output? what’s the actual output?
what’s the link to the challenge?

Sorry, have edited original post.

your issue is here:

You’ll need to remove all non-alphanumeric characters (punctuation, spaces and symbols) and turn everything into the same case (lower or upper case) in order to check for palindromes.

you are checking only for letters

1 Like