Function: checking for palindromes

Hello! I’m determined to not look up a solution and I want to solve this, even if it takes me an additional several hours! I’ve refreshed my memory on regular expressions so I think I have a better idea of where and how to apply them.

Some guidance rather than answers would be much appreciated! See below for my specific questions pertaining to the problem.

function palindrome(str) {
  str = str.replace(/s+|d+|\W+f/gi, "");
  str = str.split();
  if ((str.reverse()).toLowerCase === str.toLowerCase) {
  return true;
} else {
  return false;


Let me start off by laying out the steps for how I approached this problem to creating a function to determine if the input is a palindrome (excluding numbers, white spaces and special characters).

A) Replace all of the string’s unnecessary characters, mentioned above. Here I chose to use a regular expression within the .replace() method.
B) I know I’ll need to use the .reverse() method which is only applicable to arrays. Next step is turning the string variable into an array using .split().
C) Time to compare using .reverse() and .toLowerCase.

My specific questions/thoughts on this one:

  1. At some point should I convert str from an array back to a string using .join()?

2.The current argument shown above (“almostomla”) returns, incorrectly, as true. Other arguments that are also returning incorrectly as true are “not a palindrome”, “nope”, “1 eye for of 1 eye”, and “five|_/|four”. I can’t find similarities that would cause my code to identify these as palindromes.

  1. Something’s funky with .reverse(), and that might be what’s giving me so many problems.

Thanks for reading!

  1. Yes you’ll need to convert back to a string before comparing. Example: "hello" === "hello" would be true, but ["h", "e", "l", "l", "o"] === ["h", "e", "l", "l", "o"] would not be true, if that makes sense (you can’t compare arrays with === to see if they have the same elements in them).
  2. reverse mutates an array “in-place”, meaning when you call str.reverse() you’re actually changing str. So then when you’re comparing the two, they’re both reversed. You’re also missing some parentheses on your toLowerCase calls which is messing things up a bit too. Finally, you’re missing some backslashes (\) in your RegEx.
  3. I think if you address the above problems then reverse will work fine.

Oh yeah, take another look at the docs for split and join. Specifically read about the optional separator parameters.

Let me know if I was too vague on anything. You’ve gotten very close to the solution on your own and you definitely have the right approach to the problem. Keep it up!

1 Like

Thanks, I’m close to a solution! It’s just a matter of figuring out that damn regex format which I can’t seem to grasp. Almost there…

I got it, after an embarrassingly long time (an hour or two yesterday, and at least 3 hours today). At least I learned about regular expressions! The FCC section on it was pretty brief, and this time around I read lots more documentation for it.

There has to be a more graceful and succinct way to identify special characters, but this works for what I needed it to do right now.

Ugh I am so embarrassed that took so long but that felt so good to finally get it right!!! Thank you for your guidance!

function palindrome(str){
  str = str.replace(/\s+|\W+|[!@#$%^&*.,=+_-]+/g,""); 
var strArray = str.split("");
var reverse = strArray.join("");
if (reverse.toLowerCase() === str.toLowerCase()){
  return (true);
} else {

palindrome("race car");

Regex issues:

  1. It’s \s+, \d+.
  2. The + is unnecessary because you’re using g to replace all instances of the character in question.
  3. With ...|\W+f you are saying 1 or more of one of the options followed by a literal letter “f”.
  4. The challenge is not asking you to remove numbers, if you remove the numbers some tests will fail (ie matching on that \d is wrong).
  5. \W means any non-alphanumeric character: spaces are non-alphanumeric characters, you don’t need to look for them in addition (\s is already covered by \W, so is not needed)
  6. The underscore character _ is an alphanumeric character, you need to account for that (the \W will not match on it).
  1. Yep, made sure to have the backslash () there when appropriate.
  2. Ohh okay, makes sense.
  3. I think that was a typo, and has since been removed.
  4. The \d was removed as well when I realized that numbers were necessary.
  5. Tried it out sans \s and +'s, works just fine, thanks!
  6. Yes, accounted for in the brackets toward the end.

Thanks! I need to do some additional reading and practicing with regex.

Sorry, I didn’t see you’re second post! Re this:


All the stuff in the brackets - that’s all covered by \W apart from the underscore. Almost all C-like (and majority of non-C-like?) languages allow underscores_as_identifiers, that’s why they count. So you can just write:

replace(/\W|_/g, "")

And it’ll do the same thing

1 Like