Skip to content

Instantly share code, notes, and snippets.

@mec
Last active August 29, 2015 14:27
Show Gist options
  • Save mec/b77f8a4d6ad85de614fd to your computer and use it in GitHub Desktop.
Save mec/b77f8a4d6ad85de614fd to your computer and use it in GitHub Desktop.
Test if a word is a palindrome, the same forwards as backwards.
function isPalindrome(word) {
// split the word into array for easiness
var word1 = word.split("");
// log
console.log(word);
// last array index of the word to count backwards
var backwardsCounter = word.length - 1;
// loop over the word, once forwards, once backwards
// if any letters do not match, it's not a palindrome
// otherwise it is
for (var i = 0; i < word1.length; i++) {
// log the comparison
console.log(word[i] + " > " + word[backwardsCounter]);
// test the comparison, we make everything uppercase for simplicityness
if( word[i].toUpperCase() !== word[backwardsCounter].toUpperCase() ) return false;
// decrement the backwards counter
backwardsCounter--;
};
return true; // assume truthiness
};
result = isPalindrome("aibohphobia"); // aibohphobia – the fear of palindromes
console.log(result);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment