My code is passing all the tests except for “almostomla.” I have figured out that this is because all the other false tests are false in the first iteration of my recursive function, whereas this one is true for several levels. I cant figure out how to fix this.
I added console.log on the recursive function and this test case prints out “lmostoml”, “mostom”, “osto”, “st”. I don’t understand why when “st” is passed back to check() it is not coming back false.
Thanks for your help!
**Your code so far**
function palindrome(str) {
let newStr = str.toLowerCase().replace(/[\W_]/g, "");
console.log("Cleaned string: " + str);
function check(str) {
let n = str.length;
if (n > 1) {
if(str.charAt(0) === str.charAt(n - 1)) {
str = str.substr(1, n - 2);
console.log(str);
check(str);
} else {
console.log("is not a palindrome");
return false;
}
} else {
console.log("is a palindrome");
return true;
}
return true;
}
let result = check(newStr);
console.log(result);
return result;
}
palindrome("almostomla");
palindrome("eye");
palindrome("A man, a plan, a canal. Panama");
**Your browser information:**
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/89.0.4389.114 Safari/537.36
.
Challenge: Palindrome Checker
Link to the challenge: