Why does my Palindrome solution pass all but one test?

Why does my Palindrome solution pass all but one test?
0.0 0


Why doesn’t this work?

function palindrome(str) {
  // Good luck!
  var lowStr = str.toLowerCase();
  var clean = lowStr.replace(/[!@#$%^&\*()-=+`~,_/]/g, ' ');
  var arrayStr = clean.split(' ');
  var newStr = arrayStr.join('');
  var forwards = [];
  for (i = 0; i < newStr.length; i++) {

  var backwards = [];
  for (var i = forwards.length-1; i >= 0 ; i--) {
  if (backwards.join('') == forwards.join('')) {
    return true;
  } else {
    return false;

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

It just happens to work with all the other tests. I tested it in the browser console and it also returns ‘true’. It just doesn’t seem to make sense because it appears to work with everything else. Can anyone please explain?

Palindrome Solutions

I moved your question out of the other topic, because it was not a complete solution and instead was a question about why your particular does not pass the one test.

Your solution is removing numbers when creating the clean variable and it should not.

Under the challenge’s Note you will see:

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

Non-alphanumeric means anything that is not a letter or a number.


Thank you so much for you time. It is still a bit difficult to grasp how the numbers got removed as a beginner, but your tip and a little search on stackoverflow helped me understand this code:

var clean = lowStr.replace(/[\W_]/g, ' ');

I made this change and it worked! Thanks.


FYI - The \W part of the is like writing /[^a-zA-Z0-9]/ which means NOT a-Z, A-Z, or 0-9. The extra _ in the regular expression is needed, because regex considers the underscore character a word character (what the \W represents) and you want to replace those too.