Skip to content

Instantly share code, notes, and snippets.

@ambergkim
Created July 14, 2018 00:55
Show Gist options
  • Save ambergkim/01f93f4786c858b5ba773cb9198f1664 to your computer and use it in GitHub Desktop.
Save ambergkim/01f93f4786c858b5ba773cb9198f1664 to your computer and use it in GitHub Desktop.
check if a string is a palindrome
// 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