Last active
June 19, 2018 01:41
-
-
Save rodrigolira/99e15c790c1c7b3f63566d4507616a76 to your computer and use it in GitHub Desktop.
Detecting Palindromes in javascript
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
// Implement a function that will receive a string as a parameter and will return | |
// true or false indicating whether the text is a palindrome (spelled the same way | |
// forward and backwards). The solution should avoid the use of regular expressions. | |
function isPalindrome(text) { | |
// TODO: convert accented characters to its ASCII equivalent character | |
// so it could be used by other languages. | |
var allowedChars = "abcdefghijklmnopqrstuvwxyz0123456789"; | |
var lowercaseText = text.toLowerCase(); | |
var forwardWordArray = []; | |
for (var i=0; i < lowercaseText.length; i++) { | |
if (allowedChars.indexOf(lowercaseText[i]) >= 0) | |
forwardWordArray.push(lowercaseText[i]); | |
} | |
return forwardWordArray.join() === forwardWordArray.reverse().join(); | |
} | |
isPalindrome("race car"); | |
//isPalindrome("Madam, I'm Adam"); | |
//isPalindrome("browser"); | |
//isPalindrome("012343210"); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment