当前位置:网站首页>牛客 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>
边栏推荐
- What is the difference between JSP and servlet?
- 什么是敏捷开发流程
- Nexus簡介及小白使用IDEA打包上傳到Nexus3私服詳細教程
- 【征文活动】亲爱的开发者,RT-Thread社区喊你投稿啦
- Nexus简介及小白使用IDEA打包上传到Nexus3私服详细教程
- Listing of chaozhuo Aviation Technology Co., Ltd.: raising 900million yuan, with a market value of more than 6billion yuan, becoming the first science and technology innovation board enterprise in Xia
- Goodbye, shucang. Alibaba's data Lake construction strategy is really awesome!
- 2022 interview questions
- [web technology] 1233 seconds understand web component
- visibilitychange – 指定标签页可见时,刷新页面数据
猜你喜欢

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

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

871. Minimum refueling times

【Leetcode】13. 罗马数字转整数

福元医药上交所上市:市值105亿 胡柏藩身价超40亿

Baobab's gem IPO was terminated: Tang Guangyu once planned to raise 1.8 billion to control 47% of the equity

Experience home office, feel the completion of the project | community essay solicitation
![[essay solicitation activity] Dear developer, RT thread community calls you to contribute](/img/31/11409606718e0f4837f4cc572172a3.png)
[essay solicitation activity] Dear developer, RT thread community calls you to contribute

Win10系统使用pip安装juypter notebook过程记录(安装在系统盘以外的盘)

酒仙网IPO被终止:曾拟募资10亿 红杉与东方富海是股东
随机推荐
Role and function of uboot
What is the difference between JSP and servlet?
What is generics- Introduction to generics
2322. Remove the minimum fraction of edges from the tree (XOR and & Simulation)
默认浏览器设置不了怎么办?
云通信接口更新迭代——SUBMAIL API V4正式上线
Introduction to nexus and detailed tutorial of Xiaobai using idea to package and upload to nexus3 private server
求简单微分方程
2022 interview questions
Eth data set download and related problems
executescalar mysql_ExecuteScalar()
uniapp H5页面调用微信支付
二、mock平台的扩展
chmod命令原理及用法详解[通俗易懂]
七张图,学会做有价值的经营分析
[leetcode] 14. Préfixe public le plus long
R and rstudio download and installation tutorial (super detailed)
The poor family once again gave birth to a noble son: Jiangxi poor county got the provincial number one, what did you do right?
The difference between class and getClass ()
Chmod command principle and usage details [easy to understand]