How to begin solving the palindromes javascript?

How to begin solving the palindromes javascript?
0.0 0

#1

Hello Everyone,

When removing characters…say special characters. How can I use the regular expression method to do this task? I know what else methods to use, but I’m confused how I remove special characters using the regular expression.


#2

I recommend using replace(). For trying out your use of regular expression tokens, regex101 is very handy.


#3

Okay. I think I’ll just use replace. I’ve spent way to much time on this algorithm.


#4

I’ve been working on this for 2 hours now with no progress. I really don’t know what to do. Here is what I have. I hope you guys can help. I really don’t want to look at the solution.

function palindrome(str) {

var toLowerCaseString = str.toLowerCase();

return toLowerCaseString;

}

palindrome(“EYE!”);

I don’t know how to use the replace() function to get rid of the special characters. Any ideas on what I can do?


#5

Have a look at the following meta character used in a regular expression.

The \W special character which matches any non-word character. Equivalent to [^A-Za-z0-9_].

For example, /\W/ or /[^A-Za-z0-9_]/ matches ‘%’ in “50%.”


#6

I didn’t want to, but I found a couple answers on the Freecode camp forums. I did, however, do a Google search and found this below.

https://www.w3schools.com/jsref/jsref_obj_regexp.asp

I’m starting to see these basic methods are really powerful. You can do allot with very little code and without doing loops. Coming from a Java background this is pretty cool.


#7

Another function you could use which will come in handy a later challenge, is the charCodeAt string method. Since you only want to consider alpha-numeric characters, you could loop through the str argument and build a new string by checking to make sure it’s UTF-16 code unit value is between 0 and 9 (the numbers 0 through 9) and if you have already converted the string to lowercase, then also make sure the value is between 97 and 122 (the letters a through z). If it is not between these ranges, then do not include it in the new string. Then, you can start to validate the new string is a palindrome or not.

I personally would use the replace and regular expression, but this is another option.


#8

Thank you randelldawson! Time to get back to work.