"Reverse numbers" means outputting numbers in reverse order. For example, a number 12345, after reversal, is 54321; then in "Quickly reverse and output a positive integer through a PHP program" we explain how Reverse a number through PHP. Friends who are interested can learn about it~
The focus of this article is to explain how to reverse a number through javascript.
Without further ado, I will go directly to the code:
javascript to reverse the numbers:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title></title> </head> <body> <script> function reverse_a_number(n) { n = n + ""; return n.split("").reverse().join(""); } console.log(reverse_a_number(32243)); </script> </body> </html>
Then open the console to view the reversal result :
In the above code, I gave an example number "32243", which is obviously reversed to "34223".
In the js code in the above example, you need to master 3 methods:
1.split() method: used to split a string into a string array, its syntax is " stringObject.split(separator,howmany)
”;
parameters respectively represent:
separator,字符串或正则表达式,从该参数指定的地方分割 stringObject。 howmany,该参数可指定返回的数组的最大长度。如果设置了该参数,返回的子串不会多于这个参数指定的数组。如果没有设置该参数,整个字符串都会被分割,不考虑它的长度。
→Note: If an empty string ("") is used as separator, then each character in stringObject will be separated. String.split() performs the opposite operation of Array.join .
2.reverse() method: used to reverse the order of elements in an array, its syntax is "arrayObject.reverse()
";
→Note: This method will change the original array but not create a new array.
3.join() method: used to put all the elements in the array into a string. The elements are separated by the specified delimiter. The syntax is "arrayObject. join(separator)
"; its parameter separator specifies the separator to be used; if this parameter is omitted, a comma is used as the separator.
Finally, I would like to recommend "JavaScript Basics Tutorial" ~ Welcome everyone to learn ~
The above is the detailed content of Analysis of how to reverse numbers through javascript. For more information, please follow other related articles on the PHP Chinese website!

去掉重复并排序的方法:1、使用“Array.from(new Set(arr))”或者“[…new Set(arr)]”语句,去掉数组中的重复元素,返回去重后的新数组;2、利用sort()对去重数组进行排序,语法“去重数组.sort()”。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于Symbol类型、隐藏属性及全局注册表的相关问题,包括了Symbol类型的描述、Symbol不会隐式转字符串等问题,下面一起来看一下,希望对大家有帮助。

怎么制作文字轮播与图片轮播?大家第一想到的是不是利用js,其实利用纯CSS也能实现文字轮播与图片轮播,下面来看看实现方法,希望对大家有所帮助!

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于对象的构造函数和new操作符,构造函数是所有对象的成员方法中,最早被调用的那个,下面一起来看一下吧,希望对大家有帮助。

方法:1、利用“点击元素对象.unbind("click");”方法,该方法可以移除被选元素的事件处理程序;2、利用“点击元素对象.off("click");”方法,该方法可以移除通过on()方法添加的事件处理程序。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于面向对象的相关问题,包括了属性描述符、数据描述符、存取描述符等等内容,下面一起来看一下,希望对大家有帮助。

foreach不是es6的方法。foreach是es3中一个遍历数组的方法,可以调用数组的每个元素,并将元素传给回调函数进行处理,语法“array.forEach(function(当前元素,索引,数组){...})”;该方法不处理空数组。

本篇文章给大家带来了关于JavaScript的相关知识,其中主要介绍了关于BOM操作的相关问题,包括了window对象的常见事件、JavaScript执行机制等等相关内容,下面一起来看一下,希望对大家有帮助。


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Chinese version
Chinese version, very easy to use

SublimeText3 English version
Recommended: Win version, supports code prompts!

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver CS6
Visual web development tools

WebStorm Mac version
Useful JavaScript development tools