How to use JavaScript to judge whether it is a palindrome or not
This article mainly explains "how to use JavaScript to judge whether palindromes are palindromes". The explanation in the article is simple and clear and easy to learn and understand. Please follow the editor's train of thought to study and learn "how to use JavaScript to judge palindromes".
Methods: 1, use toString () and split () to convert numbers into arrays; 2, use reserve () to flip the order of array elements; 3, use join () and Number () to convert the flipped array into numbers; 5, use the "= =" operator to compare whether the original number is equal to the flipped number, and if so, it is a palindrome number.
The operating environment of this tutorial: windows7 system, javascript1.8.5 version, Dell G3 computer.
The number of palindromes means that the positive order (from left to right) and the reverse order (from right to left) are the same integers.
For example, "121" is a palindrome.
So how to use JavaScript to tell whether it is a palindrome or not?
Implementation idea: using arrays
Convert a number to a string first
Then split the string into characters and store them in an array, that is, into an array of characters.
Then use the reserve () method to flip the array and reverse the order of the elements in the array
Then convert the flipped array to a number
Finally, use = = to compare to see if the original number is equal to the flipped number, and if so, it is a palindrome.
Implementation code:
Var xchang121 if str = x.toString () / into the string var arr = str.split ('') / / into an array var res = Number (arr.reverse (). Join (')) if (x===res) {console.log (x + "is a palindrome");} else {console.log (x + "is not a palindrome");}
Thank you for your reading, the above is the content of "how to use JavaScript to judge whether palindromes are palindromes". After the study of this article, I believe you have a deeper understanding of how to use JavaScript to judge whether palindromes are palindromes or not. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!