当前位置:网站首页>牛客 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>
边栏推荐
- The impact of telecommuting on all aspects of our experience | community essay solicitation
- The difference of message mechanism between MFC and QT
- si446使用记录(二):使用WDS3生成头文件
- 綠竹生物沖刺港股:年期內虧損超5億 泰格醫藥與北京亦莊是股東
- traceroute命令讲解
- linux安装postgresql + patroni 集群问题
- How to transfer business data with BorgWarner through EDI?
- Believe in yourself and finish the JVM interview this time
- 2、 Expansion of mock platform
- Qstype implementation of self drawing interface project practice (II)
猜你喜欢

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

Sword finger offer 27 Image of binary tree
![[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)

si446使用记录(一):基本资料获取

七张图,学会做有价值的经营分析

对接保时捷及3PL EDI案例

Sword finger offer 21 Adjust the array order so that odd numbers precede even numbers

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

Smart trash can (V) - light up OLED

Eye of depth (II) -- matrix and its basic operations
随机推荐
【Leetcode】13. Roman numeral to integer
体验居家办公完成项目有感 | 社区征文
如何与博格华纳BorgWarner通过EDI传输业务数据?
PCL知识点——体素化网格方法对点云进行下采样
Vscode + eslint configuration
博客主题 “Text“ 夏日清新特别版
A case study of college entrance examination prediction based on multivariate time series
Nexus簡介及小白使用IDEA打包上傳到Nexus3私服詳細教程
Chapter 3 of hands on deep learning - (1) linear regression is realized from scratch_ Learning thinking and exercise answers
ETH数据集下载及相关问题
Ap和F107数据来源及处理
什么是敏捷开发流程
Domestic relatively good OJ platform [easy to understand]
Introduction to Hisilicon hi3798mv100 set top box chip [easy to understand]
智能垃圾桶(五)——点亮OLED
畅玩集团冲刺港股:年营收2.89亿 刘辉有53.46%投票权
traceroute命令讲解
Linux Installation PostgreSQL + Patroni cluster problem
The poor family once again gave birth to a noble son: Jiangxi poor county got the provincial number one, what did you do right?
ThreadLocal