Pages

Saturday, February 27, 2016

CHECK FOR PALINDROMES

 A palindrome is a word or sentence that's spelled the same way both forward and backward, ignoring punctuation, case, and spacing.

Challenge is to return true if the given string is a palindrome. Otherwise, return false.

It took me a while to solve this challenge. It could pass all the test except 

palindrome("1 eye for of 1 eye.") should return false.

The problem was I was discarding digits 0-9 in my regex expression.

I amended the regex expression from /[^a-b]/gi to /[^a-b0-9]/gi to include the digits into the stripped version.

Behold I cracked it.....

Here is my implementation:



function palindrome(str) {
  
  var stripped = str.toLowerCase().replace(/[^a-z0-9]/gi, '');
  var reversed = stripped.split('').reverse().join('');
   
  if (stripped === reversed){
    return true;
   } else {
     return false;
  }
  
}

No comments:

Post a Comment