Recently, I was watching JS Advanced programming 3 while looking for a topic to practice on FCC. That’s a blast. And it’s not just with the life in the classroom, dormitory, laboratory, library and other place will be the fifth chapter “reference type” fix, the FCC has discretion to the author to a “palindrome”, the author teeth, spent two days, and the array, and the string, and the research scope, but also for a long time regular expressions. Well, it pays off, hey hey hey, now for you to share in detail with JS to achieve accurate palindrome number discrimination!!
Let me show you some types of strings:
race car
not a palindrome
A man, a plan, a canal. Panama
My age is 0, 0 si ega ym.
0_0 (: /-\ 🙂 0-0
Letters, numbers, underscores, Spaces, dots, I don’t know what…
So our first step is to get rid of anything that isn’t numbers or letters!!
Here we use the first method, which is called replace(), and we’re going to share our ideas. If you’re not sure about replace() or some of the other methods below, check them out for yourself.
All right, so the general idea is replace(exp,””). That means match all the non-characters and get rid of them.
The question is, how does a regular expression match all non-characters?
This question is making my head spin
At first, a friend suggested that I use \s, but it turns out that it matches whitespace characters, so some symbols are unmatched
It took me some research on regular expressions and some discussion with my friends to find a way
\W+ matches all non-characters
Because \w matches numbers and letters, \w is antisense.
But that’s not all. \W doesn’t match underscores, so we have to add \_+
1 var newStr= str.replace(/\W+/g,''); NewStr = newstr.replace (/\_+/g, ""); // Match all underscores and replace with empty string 3 newStr= newstr.tolowerCase ();Copy the code
At this point, we have solved the case problem and non-character interference
Next, just use the method in the reference type to determine that it is a palindrome.
Okay, so the idea is, you take that string and you convert it into the array arr1, and then you reverse that array to get another array arR2,
Arr1 [I] ==arr2 [I]. If it’s a palindrome, then there’s no doubt that the equation works. If it’s not a palindrome, the equation doesn’t work
1 var arr1= newStr.split(''); Var arr2=arr1.reverse(); For (I =0; iCopy the code
It seems to be all right
When I ran with confidence, the browser didn't give me the answer I was expecting :(
What's the problem?
I don't know if you remember what type an array is?
Right!!!!!! Arrays are reference types!! Arr1 and ARR2 both point to the same heap.
When we write arr1.reverse(), the heap changes and is reversed!! That's the point!! So we are comparing two identical arrays in the for() loop!! (Oh My God!)
The problem is found out, how to solve?
Simple, since arrays don't work, I'll use a String from the primitive type!!
We convert the inverted array to a string and compare the two strings
var newStr2=arr2.join(''); If (newStr==newStr2){// Compare 1 return true; }else{ return false; }Copy the code
In this way, the whole idea of using JS to judge palindrome number comes out ~
Here is the complete code, you can copy down to run oh
1
2
3
4
5
6
7
8
31
32 Copy the code
End of sharing, thank you.