Write a simple function (less than 80 characters) that returns a boolean indicating whether or not a string is a palindrome.(JAVA SCRIPT)

The following one line function will return true if str is a palindrome; otherwise, it returns false.

function isPalindrome(str) {
str = str.replace(/\W/g, ”).toLowerCase();
return (str == str.split(”).reverse().join(”));
}
For example:

console.log(isPalindrome(“level”)); // logs ‘true’
console.log(isPalindrome(“levels”)); // logs ‘false’
console.log(isPalindrome(“A car, a man, a maraca”)); // logs ‘true’

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s