Palindrome Checker...stumped

Palindrome Checker...stumped
0

#1

Ok I’m stumped
My code is comparable to the Intermediate solution but I’m still not passing several of the tests
I’d appreciate any help to point me in the right direction

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

function palindrome(str) {
  var regEx = /\s\W_/g;
  var newStr = str.toLowerCase().replace(regEx, "");

  for (var i = 0; i < newStr.length/2; i++){
    if (newStr[i] !== newStr[newStr.length - 1 - i]){
      return false;
    }
  }
  console.log(newStr);

  // Good luck!
  return true;
}
palindrome("eye");

#2

The regex is not the same as any of the solutions, they are all using something you are not using. If you have looked at the solutions, look again and check your regex against them.


#3

Yes that was the problem I missed the square brackets


#4

This regular expression will only replace instances where a non-word character is immediately preceded by a white space character and immediately followed by the underscore character.

A string like “eye" would not be matched. However a string like " e ?“ye” would match the " ?_” and replace it with “” resulting in “eye”.

Also, the \s is not needed, because the \W takes care of white spaces also.

Lastly, the use of brackets for a character set is not needed either. You could use | like:

/\W|_/

#5

Thanks for the tip, adding in the square brackets solved the problem so the regEx should match spaces and non-word characters…

Also thanks for the edits


#6

I was just letting you know that spaces are non-word characters, which is why you do not have to include the \s before the \W inside the brackets.