What's wrong with my code? (checking for a palindrome)


#1
function palindrome(str) {
str = str.replace(/[^a-zA-Z0-9]/g, "");
str = str.split(" ");
if (str.join("") == str.reverse()) {
return(true);

}

else if(str.join("") != str.reverse()) {
return (false);
}
  return str;
}



palindrome("eye");`

#2

First of all, please precede and follow your code sample with three backticks (below the ESC key) so it is more readable:




As to your code, I see a few problems.

str = str.split(" ");

I think you want an empty string in there so the string is broken into an array on ever character. You want:

str = str.split("");

The other problem is the line:

if (str.join("") == str.reverse()) {

Think about it. On the left hand of the evaluation it will resolve to a string, and the right side will resolve to a reversed array. How will those equate? You need to join the reversed array too so it will be a string. (Or not join the left side, but I think strings are easier to compare than arrays.)

Also, there is no need for an else if, a simple else will do. You do not need to test for the opposite case of your if statement - that is what else does automatically.

Also, your line:

return str;

is unneeded and is unreachable code.


#3

Thanks for the help. One last thing though. I would I go about turning it into lower case? When ever I try to use “.toLowerCase()” anywhere it doesn’t seem to work.


#4

The .toLowerCase() method doesn’t alter the string unless you write like str = str.toLowerCase(). Are you doing like that? Can you post your current code so we can properly help you? (the most recent version).


#5
function palindrome(str) {
  str = str.replace(/[^a-zA-Z0-9]/g, "");
  str = str.split("");
  str = str.toLowerCase();

  if (str.join() == str.reverse().join()) {
return(true);

}

else {
return (false);
}


}



palindrome("eye");`

#6

First of all, please format your code as I described above. It is impolite to force the people helping you to read difficult to read code.

Secondly, you are trying to convert it to lowercase after you’ve converted to an array. You have to switch the order of those - you can’t convert an array to lowercase. If you fix that, it should work.

Also, just in terms of style, those first three statments can be chained, like

str = str.func1().func2().func3();

Is it necessary? No, but it is common.

Also, your if clause is largely unnecessary. str.join() == str.reverse().join() evaluates as a boolean value, so why not:

return (str.join() == str.reverse().join()):

Anytime you have an

if (x) {
  return true;
} else {
  return false;
}

you are being unnecessarily verbose. Simple

return x;

does the job


#7

Apologies for the inconveniences I highly appreciate your help.


#8

It’s cool. Just take a second to learn how to format your code (see gif above). I made the same mistake in my first few posts. Many do. Glad we were able to help with the code. That’s why the forum is here.


#9

I need a GIF like that :wink:
(though it should be avoided until the person has been told at least once :confused:)


#10

I just use the gif because it’s so clear. When people were first yelling at me to do this I didn’t understand what they meant. I don’t mean it to be hostile, but I see how it may come off that way. But it is extremely clear.

The gif can be extracted with a right-click/save-image-as


#11

I might use it next time someone doesn’t use code tags :slight_smile:


#12

Sure, I didn’t make it. I’m not sure who did. But it’s a nonstop battle to get new people to format properly and we need every toll we can get.


#13

That’s understandable :slight_smile: