Skip to content

Instantly share code, notes, and snippets.

@perjo927
Created September 13, 2020 10:52
Show Gist options
  • Save perjo927/ca1e0112f65d67591f3f9d39cd056fe4 to your computer and use it in GitHub Desktop.
Save perjo927/ca1e0112f65d67591f3f9d39cd056fe4 to your computer and use it in GitHub Desktop.
Palindrome FP Validator
const isEmpty = word => word === "";
const isSingleChar = word => word.length === 1;
const isDoubleChar = word => word.length === 2;
const firstChar = word => word[0];
const lastChar = word => word.slice(-1);
const isMatch = (a,b) => a === b;
const trimEnds = word => word.slice(1,-1);
const isEndsMatched = word => isEmpty(word) || isMatch(firstChar(word), lastChar(word))
const isPalindrome = word => {
if(!isEndsMatched(word)) {
return false;
}
if(isEmpty(word) || isSingleChar(word) || isDoubleChar(word)) {
return true;
}
return isPalindrome(trimEnds(word))
}
console.log(isPalindrome("tenet")); // true
console.log(isPalindrome("tennis")); // false
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment