Checking for Palindromes

How come the test case “race car” is passed and “never odd or even” fails?

Here is my code:
function palindrome(str) { // Good luck! var src=str.toLowerCase(); str=str.toLowerCase(); src=src.replace(/(\_|\!|\@|\#|\$|\^|\*|\-|\+|\,|\.)/gi,''); str=str.replace(/(\_|\!|\@|\#|\$|\^|\*|\-|\+|\,|\.)/gi,''); src=src.replace(' ',''); str=str.replace(' ',''); console.log(str,src); var srcc=src.split('').reverse().join(''); if(srcc==str) return true; else return false; }

There is something wrong with your regular expression…
I’m not that good with it so can’t find the exact fault but when I used mine in replacement it worked.
Try yourself with different ways first.

Oh by the way… use this for your console: console.log(srcc); and put it after the reverse join thingie to get a proper reading.

var srcc=src.split('').reverse().join('');
console.log(srcc);
console.log(str);
if(srcc==str) {
      return true;
} else{
     return false;
}

I have used the regexp for replacing all the special characters.

By the way, you don’t need to have two strings and cleaned both. You can clean the one string and then afterwards make a new string that you reverse and later compare them.

1 Like

I understand. I went the other way… instead of specifying each symbol, I specified allowing the letters and numbers /[^a-zA-Z0-9]/g

Oh yes!.
I am such a dumbo.
I guess the caret symbol complements the original regexp.
Thanks, I never thought of going the other way.

You’re gona hate yourself when you see the spoiler alert :slight_smile: … Their solution is so elegant.

Please use backticks (```) before and after your code to make it easier to read. And some spacing too. It’s impossible to decipher your code just by looking at it.

Here it is, much better looking and easier to analyse:

function palindrome(str) { 
  // Good luck! 
  var src = str.toLowerCase(); 
  str = str.toLowerCase(); 
  src = src.replace(/(\_|\!|\@|\#|\$|\^|\*|\-|\+|\,|\.)/gi,''); 
  str = str.replace(/(\_|\!|\@|\#|\$|\^|\*|\-|\+|\,|\.)/gi,''); 
  src = src.replace(' ',''); 
  str = str.replace(' ',''); 
  console.log(str,src); 
  var srcc = src.split('').reverse().join(''); 
  if ( srcc == str ) {
    return true; 
  }
  else return false; 
  
}

Now it’s clearer that you’re being redundant
(i.e.:

var src = str.toLowerCase(); 
str = str.toLowerCase(); 
1 Like

Yes,indeed.
Sorry for this time, would keep in mind from now.

I guess the above regexp does not replace underscores.

There are so many ways to solve this.
My solution was similar to yours.

My solution:
function palindrome(str) {
  // Good luck!
  var strLettersOnly = [];
  strLettersOnly = str.toLowerCase().match(/[A-Za-z0-9]/gi).join("");
  var strBackwardsLettersOnly = [];
  strBackwardsLettersOnly = str.toLowerCase().match(/[A-Za-z0-9]/gi).reverse().join("");
  if (strLettersOnly == strBackwardsLettersOnly) {
    return true;
   
}

  else 
    return false;
  
}


palindrome("not a palindrome");

But did it replace underscores?
Because for me the test case “_eye” is not passing.

You’re trying too hard to exclude things instead of focusing on what to include.
/[A-Za-z0-9]/gi will only include letters and numbers.

1 Like