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.
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