当前位置:网站首页>牛客 JS3 分隔符
牛客 JS3 分隔符
2022-07-02 17:23:00 【淹死的魚u】
描述
請補全JavaScript代碼,要求返回參數數字的千分比特分隔符字符串。
示例1
輸入:
_comma(12300)
輸出:
‘12,300’
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8>
</head>
<body>
<script type="text/javascript">
function _comma(number) {
// 補全代碼
// 負數判斷
let complex = false
let newStr = ''
if (number < 0) {
complex = true
}
str = number.toString()
// 分割字符串,返回字符串數組 ['1', '2', '3', '4', '5', '6', '7']
var arr = str.split('')
// 負數判斷,移除-號
if (complex) {
arr.shift()
}
// 將分割後的字符串數組倒置 ['7', '6', '5', '4', '3', '2', '1']
arr.reverse()
// 從後往前,進行數組增加分割符操作 ['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, ',' )
}
}
// 將分割後的字符串數組倒置 ['1', ',', '2', '3', '4', ',', '5', '6', '7']
arr.reverse()
// 特殊情况判斷,當數組首比特為','時,移除
if (arr[0] === ',') {
arr.shift()
}
arr.map((item) => {
newStr += item })
// 負數判斷,添加-號
if (complex) {
return '-' + newStr.toString()
} else {
return newStr.toString()
}
}
const num = _comma(1234567)
console.log(num)
</script>
</body>
</html>
边栏推荐
- IP address translation address segment
- Exploration of mobile application performance tools
- 13、Darknet YOLO3
- Youzan won the "top 50 Chinese enterprise cloud technology service providers" together with Tencent cloud and Alibaba cloud [easy to understand]
- Ocio V2 reverse LUT
- [web technology] 1233 seconds understand web component
- Sword finger offer 24 Reverse linked list
- Sword finger offer 25 Merge two sorted linked lists
- Use of openpose
- Introduction to nexus and detailed tutorial of Xiaobai using idea to package and upload to nexus3 private server
猜你喜欢

线性规划例题 投资的收益与风险

Qstype implementation of self drawing interface project practice (II)

13、Darknet YOLO3

博客主题 “Text“ 夏日清新特别版

Sword finger offer 27 Image of binary tree

默认浏览器设置不了怎么办?

Fuyuan medicine is listed on the Shanghai Stock Exchange: the market value is 10.5 billion, and Hu Baifan is worth more than 4billion

TCP congestion control details | 2 background

【Leetcode】13. Roman numeral to integer

超卓航科上市:募资9亿市值超60亿 成襄阳首家科创板企业
随机推荐
871. 最低加油次数
What is the difference between JSP and servlet?
PhD battle-11 preview | review and prospect backdoor attack and defense of neural network
Chmod command principle and usage details [easy to understand]
What is agile development process
Changwan group rushed to Hong Kong stocks: the annual revenue was 289million, and Liu Hui had 53.46% voting rights
Sword finger offer 22 The penultimate node in the linked list
如何与博格华纳BorgWarner通过EDI传输业务数据?
&lt; IV & gt; H264 decode output YUV file
Connect Porsche and 3PL EDI cases
宝宝巴士创业板IPO被终止:曾拟募资18亿 唐光宇控制47%股权
The impact of telecommuting on all aspects of our experience | community essay solicitation
Eye of depth (II) -- matrix and its basic operations
Timing / counter of 32 and 51 single chip microcomputer
綠竹生物沖刺港股:年期內虧損超5億 泰格醫藥與北京亦莊是股東
相信自己,这次一把搞定JVM面试
关于我
ThreadLocal
寒门再出贵子:江西穷县考出了省状元,做对了什么?
13、Darknet YOLO3