Checking for Palindromes help

Tell us what’s happening:
I’m a little confused on why my code isn’t working. I’ve been stuck on this challenge for days!
any Help or suggestions is appreciated!!

Your code so far

function palindrome(str) {
  
 var array = [];
  
  array = str.split("");
  backwards = array. reverse("");
  return backwards.join("");
}
if (backwards.join("") === palindrome (""));{
  
  return true;
}

else{
  return false; 

}

}

palindrome("nope");

Your browser information:

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

Link to the challenge:
https://www.freecodecamp.org/challenges/check-for-palindromes

You have a space between array. and reverse. Also, Array.prototype.reverse() doesn’t need any parameters. It should read:

array.reverse();

You also are returningbackwards.join("") from the function palindrome, and then ending the function declaration prematurely with a }. Later, your code seems to want palindrome(str) to return a boolean.

One more syntax/logic error: your if statement (now outside of the function palindrome(str)), is calling palindrome(str), and then following the if (condition) with a semicolon before the curly braces.

Several things to get you moving:

  1. You need to move the logic inside the function - it isn’t going to work if you have logic floating around. Everything thould be part of the palindrome function. The function should accepts a string, and return true or false. At the minute you have a function called palindrome that just reverses a string, then some logic outside it that can’t work.
  2. [minor] You don’t need to initialise an array (var array = [];) - str.split creates an array anyway, so var array = str.split("") is fine.
  3. There’s a gap here: array. reverse()
  4. related to point 1, this if (backwards.join("") === palindrome ("")) doesn’t make sense. You’ve already join-ed backwards, and you can’t then join a string. And palindrome("") is just calling the function palindrome with an empty string.

Once you’ve fixed these things, you’re still going to have a problem: something like eye will work, it’ll return true as a palindrome. But Eye is a palindrome, and that will return false. And what about A man, a plan, a canal, Panama - that’s a palindrome as well, but a fixed version of your logic will return false.

1 Like