Returning False in Palindrome Checker

Tell us what’s happening:
Why is it not returning as false when it is not a palindrome? I have inserted the return false part outside the for and if statement.

Your code so far


function palindrome(str) {
  // Good luck!
  for(let i = 0; i<=str.length; i++) {
  if(str[i].charAt(0) === str[i].charAt(str[i].length[i]-1)) {
    str.toLowerCase().split(' ').join('');
  return true;
  }
  }
  return false;
}

palindrome("eye");

Your browser information:

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

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

Your current if statement logic is not doing what you think. In the first iteration of the for loop, your str[0].charAt(0) is always going to be the same as str[i].charAt(str[i].length[i] - 1). Why? Because since str[i] is a single character, so the length will be 1 and 1 - 1 = 0. So basically, you are always comparing the first character to the first character which will always be true.

Also, not sure what the purpose of the following line is, but it is adding anything to the logic in determining what gets returned.

str.toLowerCase().split(' ').join('');