当前位置:网站首页>牛客 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>
边栏推荐
- Sword finger offer 21 Adjust the array order so that odd numbers precede even numbers
- Timing / counter of 32 and 51 single chip microcomputer
- 社交元宇宙平台Soul冲刺港股:年营收12.8亿 腾讯是股东
- Visibilitychange – refresh the page data when the specified tab is visible
- ETH数据集下载及相关问题
- 深度之眼(二)——矩阵及其基本运算
- Linux Installation PostgreSQL + Patroni cluster problem
- Error when uploading code to remote warehouse: remote origin already exists
- 简单介绍BASE64Encoder的使用
- 关于我
猜你喜欢

Does digicert SSL certificate support Chinese domain name application?

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

Goodbye, shucang. Alibaba's data Lake construction strategy is really awesome!

Tech talk activity preview | building intelligent visual products based on Amazon kVs

Qwebengineview crash and alternatives

Use of openpose

ETH数据集下载及相关问题

綠竹生物沖刺港股:年期內虧損超5億 泰格醫藥與北京亦莊是股東

相信自己,这次一把搞定JVM面试

Experience home office, feel the completion of the project | community essay solicitation
随机推荐
Win10系统使用pip安装juypter notebook过程记录(安装在系统盘以外的盘)
VMware install win10 image
Smart trash can (V) - light up OLED
linux安装postgresql + patroni 集群问题
The computer comes with software to make the background color of the picture transparent (matting white background)
剑指 Offer 21. 调整数组顺序使奇数位于偶数前面
什么是敏捷开发流程
si446使用记录(二):使用WDS3生成头文件
福元医药上交所上市:市值105亿 胡柏藩身价超40亿
Youzan won the "top 50 Chinese enterprise cloud technology service providers" together with Tencent cloud and Alibaba cloud [easy to understand]
QWebEngineView崩溃及替代方案
酒仙网IPO被终止:曾拟募资10亿 红杉与东方富海是股东
Use the API port of the bridge of knowledge and action to provide resources for partners to access
[web technology] 1233 seconds understand web component
Nexus简介及小白使用IDEA打包上传到Nexus3私服详细教程
简单介绍BASE64Encoder的使用
Experience home office, feel the completion of the project | community essay solicitation
Meanings of SNAT, DNAT and masquerade in iptables
Qstype implementation of self drawing interface project practice (II)
IP地址转换地址段