Palindromes in JS

Hello guys!

I got stuck on Basic Algorithm Scripting in “Check for Palindromes”.

I wrote a simpe script but it doesn’t work so I would be grateful if you could give me a hint.

Below, I provide the code I wrote and I can’t understand what’s wrong with it :slight_smile:

function palindrome(str) {
  var rep = str.replace(/^[0-9A-Za-z]+$/g, "").toLowerCase();
  var pal = rep.split('').reverse().join('');
  if (rep === pal) {
return true;
  } else {
    return false;


From the results I can tell that it doesn’t return true or false when there are symbols or spaces on the word.

Thank you guys in advance!!!

PS. I know that there might be a similar post but I am very interested to know what’s wrong with the specific code.

so before I point anything out, have you tried logging your strings rep and pal to see what they look like?

I asked because you only have a very minor thing you need to fix, it’s something you can instantly spot if you just simply use console.log to debug your code. How to solve it is not so obvious, but it really only require you to read a little bit more about the tools you’re using

if you don’t want to have your chrome developer tool open, I recommend using something like where the console is right there next to your code when you’re doing algorithm challenges. For regex, there are online tester for regex expressions

I’m not going to tell you what’s wrong specifically with your code. debug with the above method, and you should find the root of your problem.

I cleaned up your code.
You need to use triple backticks to post code to the forum.
See this post for details.

I had a mistake on my regexpress.
Thank you guys for your reply !!!