当前位置:网站首页>牛客 JS3 分隔符
牛客 JS3 分隔符
2022-07-02 14:51: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>
边栏推荐
- executescalar mysql_ ExecuteScalar()
- 福元医药上交所上市:市值105亿 胡柏藩身价超40亿
- 871. 最低加油次数
- 【Leetcode】13. Roman numeral to integer
- 书包网小说多线程爬虫[通俗易懂]
- 剑指 Offer 21. 调整数组顺序使奇数位于偶数前面
- Nexus简介及小白使用IDEA打包上传到Nexus3私服详细教程
- The computer comes with software to make the background color of the picture transparent (matting white background)
- ETH数据集下载及相关问题
- 默认浏览器设置不了怎么办?
猜你喜欢

移动应用性能工具探索之路

Eye of depth (III) -- determinant of matrix

Error when uploading code to remote warehouse: remote origin already exists

Sword finger offer 26 Substructure of tree

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

871. 最低加油次数

Experience home office, feel the completion of the project | community essay solicitation

Does digicert SSL certificate support Chinese domain name application?

The macrogenome microbiome knowledge you want is all here (2022.7)

简单线性规划问题
随机推荐
R and rstudio download and installation tutorial (super detailed)
How to transfer business data with BorgWarner through EDI?
13、Darknet YOLO3
Meanings of SNAT, DNAT and masquerade in iptables
Sword finger offer 26 Substructure of tree
Eth data set download and related problems
ROS知识点——消息过滤器 ( message_filters)
What is agile development process
一年顶十年
Configure ARP table entry restrictions and port security based on the interface (restrict users' private access to fool switches or illegal host access)
人生的开始
什么是敏捷开发流程
2022 interview questions
GeoServer:发布PostGIS数据源
对接保时捷及3PL EDI案例
远程办公对我们的各方面影响心得 | 社区征文
Eye of depth (II) -- matrix and its basic operations
dstat使用[通俗易懂]
Soul, a social meta universe platform, rushed to Hong Kong stocks: Tencent is a shareholder with an annual revenue of 1.28 billion
Ocio V2 reverse LUT