Wednesday, October 21, 2015

Bonfire : Check for Palindrome.

They mention here that we should remove all punctuation marks in the string.

Here are the strings  in the Bonfire test cases with punctuation marks.
"A man, a plan, a canal. Panama"
"My age is 0, 0 si ega ym."
"0_0 (: /-\ :) 0-0"


Used little bit of stackoverflow and MDN to understand .replace()
This is what I got:

Used a lot of console to debug each string and that really helped.



Code starts here

function palindrome(str) {
  
  // Good luck!
  //Removing punctuation marks and spaces in original string..
  var originalstring = str.replace(/[.,-\/#!$%\^&\*;:{}=\-_`~() " "]/gi,"").toLowerCase();
  var reversedstring = str.replace(/[.,-\/#!$%\^&\*;:{}=\-_`~() " "]/gi,"").toLowerCase().split("").reverse().join('');
  if(reversedstring === originalstring)
    {
  return true;
    }
  else
    {
      return false;
    }
}




palindrome("eye");






No comments:

Post a Comment