当前位置:网站首页>Using JS to realize a thousandth bit
Using JS to realize a thousandth bit
2022-04-23 09:11:00 【Listen to your heart】
One . Code
function fn(num) {
num = String(num)
let str = ''
for (let i = num.length - 1, j = 1; i >= 0; i--, j++) {
if (j % 3 == 0 && i != 0) {
str = str + num[i] + ','
continue
} else {
str = str + num[i]
}
}
return str.split('').reverse().join('')
}
let a1 = fn(1498498415)
console.log(a1);
view View :

Analysis of ideas :
It can be seen that the incoming number is first converted into a string .
i It's a decreasing form that leads to traversal
j Is an incremental form , Used to add... In place , Segmentation .
The steps of circular execution :
5
5+1
5+1+4,
5+1+4,+8
5+1+4,+8+9,
5+1+4,+8+9+4,
......
So follow this step .
then :
return str.split('').reverse().join('')
Cut strings into arrays , Flip , Convert to string .
Two . If not continue What will happen?
continue, Jump out of current loop , Execute next cycle .
Code :
<script>
function fn(num) {
num = String(num)
let str = ''
for (let i = num.length - 1, j = 1; i >= 0; i--, j++) {
if (j % 3 == 0 && i != 0) {
str = str + num[i] + ','
// continue
} else {
str = str + num[i]
}
}
return str.split('').reverse().join('')
}
let a1 = fn(1234567890)
console.log(a1);
</script>
It won't have any impact .
3、 ... and .i It's not equal to 0
stay j=3 When , here , Also to the third place , And split it up .
The answer is to add limiting conditions .
Because the format of the final result is :
![]()
That is, the first one does not need to be segmented . therefore i Can not be equal to 0.
Did you stop learning ?
版权声明
本文为[Listen to your heart]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230656262775.html
边栏推荐
- [in-depth good article] detailed explanation of Flink SQL streaming batch integration technology (I)
- Enterprise wechat application authorization / silent login
- Star Trek's strong attack opens the dream linkage between metacosmic virtual reality
- Failed to download esp32 program, prompting timeout
- Data visualization: use Excel to make radar chart
- 机器学习(六)——贝叶斯分类器
- 2022-04-22 openebs cloud native storage
- Notes on xctf questions
- GoLand debug go use - white record
- Go language self-study series | initialization of golang structure
猜你喜欢

MySQL小练习(仅适合初学者,非初学者勿进)

I don't understand time, timestamp and time zone. Look at this article

First principle mind map
![[C language] document operation](/img/89/b19dda13d27e37fedf6736c102245b.png)
[C language] document operation

Resource packaging dependency tree
![[SQL Server fast track] view and cursor of database](/img/2c/8edd92ecef11932c982db56af76c3f.png)
[SQL Server fast track] view and cursor of database

Common errors of VMware building es8

Introduction to GUI programming swing

To remember the composition ~ the pre order traversal of binary tree

Please arrange star trek in advance to break through the new playing method of chain tour, and the market heat continues to rise
随机推荐
Trc20 fund collection solution based on thinkphp5 version
Notes on xctf questions
npm ERR! network
web页面如何渲染
Concave hull acquisition method based on convex hull of point cloud
The K neighbors of each sample are obtained by packet switching
DJ music management software pioneer DJ rekordbox
Get trustedinstaller permission
ATSS(CVPR2020)
Output first order traversal according to second order and middle order traversal (25 points)
Program, process, thread; Memory structure diagram; Thread creation and startup; Common methods of thread
node安装
kettle实验
A must see wechat applet development guide 1 - basic knowledge
Wechat applet catchtap = "todetail" event problem
Single chip microcomputer nixie tube stopwatch
LeetCode_DFS_中等_1254. 统计封闭岛屿的数目
108. 将有序数组转换为二叉搜索树
Arbre de dépendance de l'emballage des ressources
[58] length of the last word [leetcode]