Created
February 10, 2022 16:50
-
-
Save dev-sampsonorson/344a330f4884fde2a591299b603e863f to your computer and use it in GitHub Desktop.
Recursive implementation to verify if a string is a palindrome
This file contains 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
let input1 = 'kayak'; | |
let input2 = 'vay ak'; | |
let input3 = 'kay ak'; | |
function isPalindrome(data) { | |
var charsToIgnore = /[.,'!?\- \"]/g; | |
var cleanData = data.replace(charsToIgnore, '').toLowerCase();; | |
if (cleanData.length === 1) return true; | |
if (cleanData.length === 2) return false; | |
if (cleanData.charAt(0) === cleanData.charAt(cleanData.length - 1)) { | |
return isPalindrome(cleanData.slice(1, -1)); | |
} | |
return false; | |
} | |
console.log(isPalindrome(input1)); | |
console.log(isPalindrome(input2)); | |
console.log(isPalindrome(input3)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment