当前位置:网站首页>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
边栏推荐
- Project upload part
- Open services in the bottom bar of idea
- DJ music management software pioneer DJ rekordbox
- Group Backpack
- [indexof] [lastIndexOf] [split] [substring] usage details
- [SQL Server fast track] view and cursor of database
- Kettle实验 (三)
- Is Zhongyan futures safe and reliable?
- Go language self-study series | golang nested structure
- Brief steps to build a website / application using flash and H5
猜你喜欢
valgrind和kcachegrind使用運行分析
A must see wechat applet development guide 1 - basic knowledge
小程序报错 :should have url attribute when using navigateTo, redirectTo or switchTab
L2-022 重排链表 (25 分)(map+结构体模拟)
错题汇总1
First principle mind map
Open services in the bottom bar of idea
Please arrange star trek in advance to break through the new playing method of chain tour, and the market heat continues to rise
108. Convert an ordered array into a binary search tree
LeetCode_ DFS_ Medium_ 1254. Count the number of closed islands
随机推荐
Applet error: should have URL attribute when using navigateto, redirectto or switchtab
Applet error: cannot read property'currenttarget'of undefined
[original] use system Text. JSON formats the JSON string
Little girl walking
Trc20 fund collection solution based on thinkphp5 version
Brush classic topics
L2-024 tribe (25 points) (and check the collection)
Redis Desktop Manager for Mac
Kettle实验
资源打包关系依赖树
js 原型链的深入
NPM installation yarn
GoLand debug go use - white record
LeetCode_DFS_中等_1254. 统计封闭岛屿的数目
Resource packaging dependency tree
Thread scheduling (priority)
JS prototype chain
错题汇总1
搞不懂时间、时间戳、时区,快来看这篇
Technological innovation in government affairs in the construction of Digital Government