Skip to content

Instantly share code, notes, and snippets.

@mikepenzin
Created February 28, 2017 15:01
Show Gist options
  • Save mikepenzin/c7c95f1d069e24ad321ab515e082be21 to your computer and use it in GitHub Desktop.
Save mikepenzin/c7c95f1d069e24ad321ab515e082be21 to your computer and use it in GitHub Desktop.
The following function will return true if str is a palindrome; otherwise, it returns false.
// isPalindrome("level"); // Outputs true
// isPalindrome("levels"); // Outputs false
// isPalindrome("A car, a man, a maraca"); // Outputs true
function isPalindrome(str) {
// We will replace any non-word character with empty place.
// Lowercase all string
str = str.replace(/\W/g, '').toLowerCase();
// First use split in order to convert string to array.
// So, every single character is different array instance.
// Reverse the array. Use join to convert array into string.
console.log(str == str.split('').reverse().join(''));
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment