How To Check For Palindromes Using JavaScript

I figured this out while finishing a lesson on FreeCodeCamp.com. There's a good chance that's why you're here too, or maybe you're trying to figure out a bit of code for an app your building.

Here's the code that I ended up with. Did you end up with a simpler solution? I'd love to hear about it, just give me a shout out @stevelongoria!

function checkPalindrome(str) {
  // Remove all non alphanumeric characters from string
  str = str.replace(/\W/g, '');
  // Make string all lowercase
  str = str.toLowerCase();
  // Reverse string and store in new variable
  var strReverse = str.split('').reverse().join('');
  // Check to see if it's a palindrome using if/else statement
    if (strReverse === str){
      return true;
      } else {
      return false;
   };
};

checkPalindrome("A man, a plan, a canal. Panama");
Show Comments

var.pop( )

Web Development Tips & Tricks