Created
February 28, 2017 15:01
-
-
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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