Skip to content

Instantly share code, notes, and snippets.

@rodrigolira
Last active June 19, 2018 01:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save rodrigolira/99e15c790c1c7b3f63566d4507616a76 to your computer and use it in GitHub Desktop.
Save rodrigolira/99e15c790c1c7b3f63566d4507616a76 to your computer and use it in GitHub Desktop.
Detecting Palindromes in javascript
// 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