Created
July 14, 2018 00:55
-
-
Save ambergkim/01f93f4786c858b5ba773cb9198f1664 to your computer and use it in GitHub Desktop.
check if a string is a palindrome
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
// Input: str | |
// Output: true or false | |
// Example: madam -> 5 length, odd | |
// Example: wowwow -> 6 length, even | |
// Example: hello -> fail, odd | |
// Example: hi -> fail, even | |
// isPalindrome(str) | |
// midpoint str/2 ceil -1 | |
// for loop until <= midpoint | |
// up = str[i] | |
// down str[length -1 -i] | |
// if not equal, return false | |
// return true | |
function isPalindrome(str) { | |
if (str === 0 || str.length === 1) { | |
return true; | |
} | |
for (let i = 0; i <= mid; i++) { | |
let up = str[i]; | |
let down = str[str.length - 1 - i]; | |
if (up !== down) { | |
return false; | |
} | |
} | |
return true; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment