Breaking

2017/07/30

Check for Palindrome using Javascript

Check for palindrome using JavaScript, this is what about this Article today.
I'm going to explain a short and best way to check for palindrom.


First the string have to converted to lower case, and the characters that are not the alphabet have to be removed,

  str = str.toLowerCase().replace(/[^a-z0-9]+/g,"");

So the string comparison becomes a array, then invert it and conveert it to string again.

  
return str == str.split('').reverse().join('');
 
And Finally the function look like this :

function palindrome(str) {
  str = str.toLowerCase().replace(/[^a-z0-9]+/g,"");
  return str == str.split('').reverse().join('');
  
}

No comments:

Post a Comment