Created
April 21, 2016 19:58
-
-
Save dadeg/cd81b216b2cd9f3173c8b59d78433e02 to your computer and use it in GitHub Desktop.
javascript function for determining if a string is a palindrome.
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
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