Help me, please!!! I do not know to whom to address: (
I can not pass the task of “Check for Palindromes” (https://www.freecodecamp.org/challenges/check-for-palindromes)
I wrote the code, everything works except for the line “palindrome(“1 eye for of 1 eye.”) should return false”.
At me the code produces “true”, and in a condition it is written, that should write “false”. Why? “1 eye for of 1 eye.” - this is a palindrome, is not it? Maybe it’s a mistake in the condition?
My code:
<p>
function palindrome(str) {
var result = str.match(/\D+/g);
result = result.join('');
result = result.match(/\w+/g);
result = result.join('');
result = result.match(/\S+/g);
result = result.join('');
result = result.replace (/_/g, '');
result = result.replace (/#/g, '');
result = result.replace (/\*/g, '');
result = result.replace (/-/g, '');
result = result.replace (/:/g, '');
result = result.replace (/|/g, '');
result = result.toLowerCase();
var resultB = result.split('').reverse();
resultB = resultB.join('');
if (resultB === result) {
return true;
} else{
return false;
}
}
</p>
So, when defining result you used str.match(/\D+/g). What this did was remove numbers from the string. It wasn’t a problem with the test case that was just numbers and symbols because your code removes all non-alphanumeric characters. That results in an empty string, which is truthy (more on that here).
What I did to fix your code was initialize result as [str]. So now var result = [str];
But I also had the inclination to answer this question the way you did and then I realized there was an easier way. Instead of targeting special characters individually and replacing them with nothing, you can target all non-alphanumeric characters and underscores with “/[\W_]/g”.
For example, here’s how I solved this question
function palindrome(str) {
return str.replace(/[\W_]/g, "").toLowerCase() ===
str.replace(/[\W_]/g, "").toLowerCase().split("").reverse().join("");
}
palindrome("eye");
If I can explain anything better, please let me know. Happy coding!
Thank you very much for your answer! I’m just starting to code and especially I want to learn how to write beautiful concise code. I am self-taught and I do not have one who will help me. I really appreciate it!
@Elen - One suggestion that might be helpful, if you want to get some help in real time, is to use the freeCodeCamp chat rooms. It used to be a topic that was introduced early on in the program, but has been somewhat demoted as a method for getting help. The chat rooms aren’t as well utilized as they used to be, when their existence was more prominently advertised. But it is still alive. If you go to the top-level “help” chat room at freeCodeCamp gitter Help chat room (you might have to sign up for the chat rooms through the available links) you will find some people available at various times, more than willing to help guide you through your solutions and answer questions for you. There are many official chat rooms - the interesting ones at the start are the help room, the JavaScript room, and the FrontEnd room.
Hope to see you over there when you are looking for help!
Thank you! I will definitely be there