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.
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:
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.
Something’s funky with .reverse(), and that might be what’s giving me so many problems.
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).
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.
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!
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("");
strArray.reverse("");
var reverse = strArray.join("");
if (reverse.toLowerCase() === str.toLowerCase()){
return (true);
} else {
return(false);
}
}
palindrome("race car");
The + is unnecessary because you’re using g to replace all instances of the character in question.
With ...|\W+f you are saying 1 or more of one of the options followed by a literal letter “f”.
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).
\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)
The underscore character _ is an alphanumeric character, you need to account for that (the \W will not match on it).
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: