JavaScript Algorithms and Data Structures Projects - Palindrome Checker

Tell us what’s happening:
My code so far splits the given input by all non-alphanumeric characters or underscores, , then recombines it back into a string made of just the letters.

As far as I can tell, the nested FOR loops in the bottom half should compare the values of the string from the outside in, returning FALSE if any letters don’t match the corresponding letter from the other end.

The only test case this code doesn’t solve for is the “almostomla” case, can anybody enlighten me as to why?

Thanks for your time.

  **Your code so far**
function palindrome(str) {
let regEx = /[^\w]|_/gi
let lowerCase = str
  .toLowerCase()
  .split(regEx)
  .join("")
  //.split("")
  //.map(item => item.charCodeAt());

console.log(lowerCase)

for (let i = 0; i < lowerCase.length; i++) {
  for (let j = lowerCase.length - 1; j > 0; j--) {
    return lowerCase[i] === lowerCase[j];
  } 
}

}

console.log(palindrome("almostomla"));
  **Your browser information:**

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

Challenge: JavaScript Algorithms and Data Structures Projects - Palindrome Checker

Link to the challenge:

This might help you see some issues:

function palindrome(str) {
  let regEx = /[^\w]|_/gi
  let lowerCase = str
    .toLowerCase()
    .split(regEx)
    .join("")
    //.split("")
    //.map(item => item.charCodeAt());

  console.log(lowerCase)
  let check = 0
  for (let i = 0; i < lowerCase.length; i++) {
    console.log('i', i)
    for (let j = lowerCase.length - 1; j > 0; j--) {
      console.log('j', j)
      console.log('check', ++check)
      return lowerCase[i] === lowerCase[j];
    } 
  }
}

Some things to consider.

  1. A 'return` exits the current function.
  2. You don’t need two loops here. You don’t need to check every letter against every other letter. You need to check the first against the last, the second against the second last, … We can do that with one loop, right? It’s just a little math.
  3. Given #2, do you need to loop the entire string? Think about it.

With your help I realized that the problem with the nested loops was that the “j” loop would exit the function once it realized that the last letter and the first letter matched, and would not compare the rest of the input.

In addition, even if it did not exit the function early, the nested loops would compare the first letter designated in the “i” loop to every letter working backwards in the “j” loop before iterating “i”, so that code wouldn’t have worked anyway.

I changed the code to the following, which solved the issue and the challenge. Thanks for your help!

function palindrome(str) {
let regEx = /[^\w]|_/gi
let lowerCase = str
.toLowerCase()
.split(regEx)
.join(“”)
//.split(“”)
//.map(item => item.charCodeAt());

console.log(lowerCase)

for (let i = 0; i < lowerCase.length; i++) {
console.log(‘start’, lowerCase[i], ‘end’, lowerCase[lowerCase.length-1-i]);
if (lowerCase[i] !== lowerCase[lowerCase.length-1-i]) {
return false;
}
} return true;
}

console.log(palindrome(“almostomla”));

1 Like

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