Skip to content

Instantly share code, notes, and snippets.

@juanigallo
Created September 8, 2017 19:56
Show Gist options
  • Save juanigallo/f7a4851835aa35a316ac0798a3867534 to your computer and use it in GitHub Desktop.
Save juanigallo/f7a4851835aa35a316ac0798a3867534 to your computer and use it in GitHub Desktop.
Check if a string is a palindrome. JavaScript implementation
//v1 without built in functions
function palindrome(str) {
var len = str.length,
mid = Math.floor(len/2);
for ( var i = 0; i < mid; i++ ) {
if (str[i] !== str[len - 1 - i]) {
return false;
}
}
return true;
}
//v2 with built in functions
function isPalindrome(s) {
return s == s.split("").reverse().join("") ? true : false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment