Skip to content

Instantly share code, notes, and snippets.

@dadeg
Created April 21, 2016 19:58
Show Gist options
  • Save dadeg/cd81b216b2cd9f3173c8b59d78433e02 to your computer and use it in GitHub Desktop.
Save dadeg/cd81b216b2cd9f3173c8b59d78433e02 to your computer and use it in GitHub Desktop.
javascript function for determining if a string is a palindrome.
var isPalindrome = (input) => {
if (typeof input !== 'string' && !Array.isArray(input)) { throw "isPalindrome() requires a string or array as an argument."; }
if (typeof input === 'string') { input = input.split(""); }
let one = input.pop();
let two = input.shift();
if (two === undefined) { return true; }
if ( one === two ) { return isPalindrome(input); }
return false;
}
console.log(isPalindrome('a')); // true
console.log(isPalindrome('aa')); //true
console.log(isPalindrome('aad')); //false
console.log(isPalindrome('aada')); //false
console.log(isPalindrome('aadaa')); //true
console.log(isPalindrome('aaddaa')); //true
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment