Palindrome Checker - Just one is failing

Tell us what’s happening:

Okay, I’ve managed to clean up the string using the first 3 processes (maybe a little long but it work)

I’m also managing to successfully check everything except for one string… “almostomla”

When I log what is happening, my loop seems to get 4 digits in and then just stops and labels it a palindrome. I can’t figure out why it isn’t progressing past halfway, my other strings all go to the end.

Does anyone have any ideas?

Your code so far


function palindrome(str) {
  let removeSpace = str.replace(/\s/g, "");

  let removeChar = removeSpace.replace(/[/,_.-]/ig, "");

  let toUpper = removeChar.toUpperCase();
  console.log(toUpper);

let i;

for(i = 0; i < toUpper.length; i++){
  
if(toUpper[i] != toUpper[toUpper.length-(i+1)]){
return false;
} else{
  return true;
}
}
}

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_14_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/79.0.3945.88 Safari/537.36.

Challenge: Palindrome Checker

Link to the challenge:
https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/javascript-algorithms-and-data-structures-projects/palindrome-checker

you are checking only the first and last letter of the string, not others. (helloh returns true in your function)
If I say this and quote the issue, can you figure out the rest yourself?

I messed up with “return”, it breaks the loop.

Me and “return” go way back, thanks, I’ll go back and kick his ass.

function palindrome(str) {
  let removeSpace = str.replace(/\s/g, "");

  let removeChar = removeSpace.replace(/[/)(,_.-]/ig, "");

  let toUpper = removeChar.toUpperCase();
  console.log(toUpper);

let i;

for(i = 0; i < toUpper.length; i++){
if(toUpper[i] != toUpper[toUpper.length-(i+1)]){
  return false;
}
}
return true;
}

Fixed! Thanks :slight_smile: