当前位置:网站首页>Niuke js3 separator
Niuke js3 separator
2022-07-02 17:23:00 【Drowned fish u】
describe
Please complete it JavaScript Code , The thousand separator string of the parameter number is required to be returned .
Example 1
Input :
_comma(12300)
Output :
‘12,300’
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8>
</head>
<body>
<script type="text/javascript">
function _comma(number) {
// The completion code
// Negative judgment
let complex = false
let newStr = ''
if (number < 0) {
complex = true
}
str = number.toString()
// Split string , Return string array ['1', '2', '3', '4', '5', '6', '7']
var arr = str.split('')
// Negative judgment , remove - Number
if (complex) {
arr.shift()
}
// Invert the split string array ['7', '6', '5', '4', '3', '2', '1']
arr.reverse()
// From back to front , Add a delimiter to the array ['7', '6', '5', ',', '4', '3', '2', ',' ,'1']
for (let i = arr.length; i >= 0; i--) {
if ( i % 3 === 0 && i !== 0 ) {
arr.splice( i, 0, ',' )
}
}
// Invert the split string array ['1', ',', '2', '3', '4', ',', '5', '6', '7']
arr.reverse()
// Judgment of special circumstances , When the first row of the array is ',' when , remove
if (arr[0] === ',') {
arr.shift()
}
arr.map((item) => {
newStr += item })
// Negative judgment , add to - Number
if (complex) {
return '-' + newStr.toString()
} else {
return newStr.toString()
}
}
const num = _comma(1234567)
console.log(num)
</script>
</body>
</html>
边栏推荐
- Timing / counter of 32 and 51 single chip microcomputer
- 简单线性规划问题
- 伟立控股港交所上市:市值5亿港元 为湖北贡献一个IPO
- 书包网小说多线程爬虫[通俗易懂]
- Vscode setting delete line shortcut [easy to understand]
- ROS知识点——消息过滤器 ( message_filters)
- ThreadLocal
- Schoolbag novel multithreaded crawler [easy to understand]
- JS delete substring in string
- 几行代码搞定RPC服务注册和发现
猜你喜欢
![[fluent] dart data type map type (create map set | initialize map set | traverse map set)](/img/02/75d21470ea0ae4cd3d17696a93d1ca.jpg)
[fluent] dart data type map type (create map set | initialize map set | traverse map set)

What if the default browser cannot be set?

Use of openpose

Weili holdings listed on the Hong Kong Stock Exchange: with a market value of HK $500million, it contributed an IPO to Hubei

【征文活动】亲爱的开发者,RT-Thread社区喊你投稿啦

绿竹生物冲刺港股:年期内亏损超5亿 泰格医药与北京亦庄是股东

伟立控股港交所上市:市值5亿港元 为湖北贡献一个IPO

社交元宇宙平台Soul冲刺港股:年营收12.8亿 腾讯是股东

Sword finger offer 25 Merge two sorted linked lists

Soul, a social meta universe platform, rushed to Hong Kong stocks: Tencent is a shareholder with an annual revenue of 1.28 billion
随机推荐
2022 interview questions
13、Darknet YOLO3
[web technology] 1233 seconds understand web component
Timing / counter of 32 and 51 single chip microcomputer
Tech talk activity preview | building intelligent visual products based on Amazon kVs
ssb门限_SSB调制「建议收藏」
Domestic relatively good OJ platform [easy to understand]
ThreadLocal
871. 最低加油次数
Linux Installation PostgreSQL + Patroni cluster problem
Sword finger offer 26 Substructure of tree
ROS知识点——ros::NodeHandle n 和 nh(“~“)的区别
How to transfer business data with BorgWarner through EDI?
Visibilitychange – refresh the page data when the specified tab is visible
Sword finger offer 24 Reverse linked list
剑指 Offer 26. 树的子结构
QWebEngineView崩溃及替代方案
ceph 原理
智能垃圾桶(五)——点亮OLED
Exploration of mobile application performance tools