Created
September 13, 2020 10:52
-
-
Save perjo927/ca1e0112f65d67591f3f9d39cd056fe4 to your computer and use it in GitHub Desktop.
Palindrome FP Validator
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
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