# Palindromes exercise

Palindromes exercise
0

#1

The code doesn’t pass. Here is my code ,i;ll explain the logic(my logic) after i post it:

``````function palindrome(str) {
var  x = str.split('').reverse().join("");
var y = x.replace(/\s+/g, '');
var palindr = y.toLowerCase();
if ( palindr == str){
return true;
}
else {
return false;
}
}
palindrome("eye");
``````

So my logic is ,take the word passed into it,turn it into an array turn it backwards then put it together back into a string,then,check if the thing backwards is the same as the word passed in there. Why it doesn’t work is my question or what different approach should i take?

#2

You’re checking if a copy of the string that has been reversed, lowercased and had all spaces removed is the same as the original string. But the task is not asking you to check if it is the same as the original string. Think about this: it will only work for very simple palindromes (“eye”). But “Eye” is a palindrome aswell. And “A man, a plan, a canal, Panama” is a palindrome as well. Also as far as I can remember, this particular task accepts numbers but ignores other non-letter characters: I think the rules of it would allow something like “&-+e!!!1???#)_(-+#e” as a palindrome

#3
``````function palindrome(str) {
var  x = str.split('').reverse().join('');
var y = x.replace(/[\W_]/g, '');
var palindr = y.toLowerCase();
if ( palindr == str){
return true;
}
else {
return false;
}
}

palindrome("eye");
``````

changed it like that,this way only a-z chars are allowed. even their solution to the quiz use the “/[\W_]/g,” in replace,so why would their work and not mine .
their solution is

``````function palindrome(str) {
return str.replace(/[\W_]/g, '').toLowerCase() ===
str.replace(/[\W_]/g, '').toLowerCase().split('').reverse().join('');
}
``````

#4

Again, the task does not require you to compare the original string to the reversed version, but that is what you are doing and it wont work

#5

#6

“Eye” is a palindrome. Your version will try to compare the original string “Eye” to “eye” so it will fail.

“A man, a plan, a canal, Panama” will compare to “amanaplanacanalpanama” so will fail.

#7

I’ve edited your post for readability. When you enter a code block into the forum, remember to precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (`</>`) will also add backticks around text.

#8

Like @DanCouper has already advised, you should not be comparing anything to the original str, but your current solution does this with the following line:

``````  if ( palindr == str){
``````

I put your code in a repl.it and then added some console.log statements, so you can see what each variable is during the execution of your code with a specific example. The example below used palindrome(‘ey-e’); which should result in true, but your solution returns false, because the values of palindr and str are not equal.