Created
September 8, 2017 19:56
-
-
Save juanigallo/f7a4851835aa35a316ac0798a3867534 to your computer and use it in GitHub Desktop.
Check if a string is a palindrome. JavaScript implementation
This file contains hidden or 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
//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