-
-
Save aliaksandr-s/e7ba88cb897c91c5b8902cc709f40393 to your computer and use it in GitHub Desktop.
isPalindrome function
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 str = "А по морде ведром - опа!"; | |
const isPalindrome = (str) => { | |
const getLetter = (n, list) => { | |
return list[n].toLowerCase(); | |
} | |
const helper = (firstNum, lastNum, list) => { | |
const testExp = /[^a-zA-Za-яА-Я]/; | |
if (testExp.test(getLetter(firstNum, list)) ) { | |
return helper(firstNum + 1, lastNum, list) | |
} else if (testExp.test(getLetter(lastNum, list))) { | |
return helper(firstNum, lastNum - 1, list) | |
} | |
if (getLetter(firstNum, list) !== getLetter(lastNum, list)) { | |
return false; | |
} | |
if (lastNum === 0) { | |
return true; | |
} | |
return helper(firstNum + 1, lastNum - 1, list) | |
} | |
return helper(0, str.length - 1, [...str]) | |
} | |
console.log(isPalindrome(str)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment