JavaScript Algorithms and Data Structures Projects - Palindrome Checker

Tell us what’s happening:
My code runs every example input well except for the “almostmla” input. Whenever a string is of length 10 and is close to a palindrome, the function returns true when it should obviously return false.

Your code so far

function palindrome(str) {
  let regex = /[\W_]/g;
  let replaceStr = str.replace(regex, '');
  let lowerCaseStr = replaceStr.toLowerCase();
  let status = true;
  
  for (let i = 0; i < lowerCaseStr.length; i++){
    if (lowerCaseStr[i] !== 
    lowerCaseStr[lowerCaseStr.length - 1 - i]) {
      status = false;
    } else {
      status = true;
    } 
  }return status
}

console.log(palindrome("almostmla"));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/113.0.0.0 Safari/537.36

Challenge: JavaScript Algorithms and Data Structures Projects - Palindrome Checker

Link to the challenge:

Have you tried following that test case in code by hand? Take a look how status changes within the for loop.

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