Check for Palindromes sutiable?

Check for Palindromes sutiable?
0

#1

Tell us what’s happening:
I got this code to work but wondering if it is sutiable as it was different from all the given solutions

Your code so far

function palindrome(str) {
  // Good luck!
  var y = str.toLowerCase('').replace(/[^a-zA-Z0-9]/g,'');
  var x = str.split('').reverse('').join('').toLowerCase('').replace(/[^a-zA-Z0-9]/g,'');
  if (x === y){
    return true;
     }
  else {
    return false;
  
}

}


palindrome("race car");

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.95 Safari/537.36.

Link to the challenge:


#2

If it does what it’s supposed to do, then you’re probably OK.

If I may add, you can reduce the if-else block to just return x === y;.

.lowerCase and .reverse don’t need to accept any arguments. And since the string is lowercase anyway, you can reduce your regex to just /[^a-z0-9]/g.

You can also reuse the y string instead of calling .toLowerCase and .replace twice.

var x = y.split('').reverse().join('');