Returns with recursion in Palindrome Checker

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:

It works fine, it does return false, so I suspect it’s not what you think it is, I think it may just be a glitch that’s to do with the browser. Have you tried refreshing your browser? Or try checking it in different browser to see if that pushes it through. Or possibly there’s some extra code you’ve got in there that doesn’t look like it should be interfering with the test but is – if that’s the case, delete that code and run the test again

Edit, sorry, what @ArielLeslie said, I’d added that to the code when I was formatting it

You’re not doing anything with the results of this call. You need to return it.

1 Like

That did it! Thanks!

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.