Need help with Palindrome challenge

Need help with Palindrome challenge
0

#1

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?


What happened to the help button that would lead to the chat page?
#2

I moved your question to a new topic. Please do not ask a completely unrelated question in another topic. Always start a new topic for questions such questions.

If you can post your code in a reply to this post, then we can take a look at what you have and help you out. When you enter a code block into the forum, 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.


#3

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>

#4

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!


#5

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!


#6

@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!


#7

Thank you! I will definitely be there :slight_smile: