What is wrong w/ this code

Javascript

function palindrome(str) {
  // Good luck!
 var a = str.replace(/\s|[0-9_]|\W|[#$%^&*()]/g, "").toLowerCase();
  if (a === a.split("").reverse().join("")) {
  return true;
  }
  return false;
}

palindrome("eye");

palindrome(“1 eye for of 1 eye.”) Why it should FALSE? for me it is returning TRUE and is true

Note the wording of the challenge:

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

In other words, for the purpose of this challenge, numbers are treated as part of the phrase.

With that in mind, you can make your regex much simpler (hint: in regexes, ^ inside a set means anything not in that set).

1 Like

Hi.

It is false, because 1eyeforof1eye != eye1forofeye1. Is it clear?

We should remove numbers, as task says

all non-alphanumeric characters (punctuation, spaces and symbols)

You have to remove everything except numbers and letters.

UPD: List of Non-Alphanumeric Tokens

did you mean to change all code of str.replace(/\s|[0-9_]|\W|[#$%^&*()]/g, "").toLowerCase(); with this ; (/\W/g, "")

This will not work, because the \w metacharacter is used to find a word character. A word character is a character from a-z, A-Z, 0-9, including the _ (underscore) character.

1 Like

have solved it like; Thanks a lot for detailed respond;

function palindrome(str) {
  // Good luck!
 var a = str.replace(/[^a-z0-9]/ig, "").toLowerCase();
  if (a === a.split("").reverse().join("")) {
  return true;
  }
  return false;
}

palindrome("eye");

Happy to help)

Good luck with algorithms!