当前位置:网站首页>Niuke js3 separator
Niuke js3 separator
2022-07-02 17:23:00 【Drowned fish u】
describe
Please complete it JavaScript Code , The thousand separator string of the parameter number is required to be returned .
Example 1
Input :
_comma(12300)
Output :
‘12,300’
<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8>
</head>
<body>
<script type="text/javascript">
function _comma(number) {
// The completion code
// Negative judgment
let complex = false
let newStr = ''
if (number < 0) {
complex = true
}
str = number.toString()
// Split string , Return string array ['1', '2', '3', '4', '5', '6', '7']
var arr = str.split('')
// Negative judgment , remove - Number
if (complex) {
arr.shift()
}
// Invert the split string array ['7', '6', '5', '4', '3', '2', '1']
arr.reverse()
// From back to front , Add a delimiter to the array ['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, ',' )
}
}
// Invert the split string array ['1', ',', '2', '3', '4', ',', '5', '6', '7']
arr.reverse()
// Judgment of special circumstances , When the first row of the array is ',' when , remove
if (arr[0] === ',') {
arr.shift()
}
arr.map((item) => {
newStr += item })
// Negative judgment , add to - Number
if (complex) {
return '-' + newStr.toString()
} else {
return newStr.toString()
}
}
const num = _comma(1234567)
console.log(num)
</script>
</body>
</html>
边栏推荐
- uniapp H5页面调用微信支付
- SSB threshold_ SSB modulation "suggestions collection"
- Experience home office, feel the completion of the project | community essay solicitation
- 伟立控股港交所上市:市值5亿港元 为湖北贡献一个IPO
- Exploration of mobile application performance tools
- 深度之眼(二)——矩阵及其基本运算
- 海思Hi3798MV100机顶盒芯片介绍[通俗易懂]
- Linux Installation PostgreSQL + Patroni cluster problem
- 对接保时捷及3PL EDI案例
- Error when uploading code to remote warehouse: remote origin already exists
猜你喜欢

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

Understand one article: four types of data index system

TCP拥塞控制详解 | 2. 背景

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

Win10 system uses pip to install juypter notebook process record (installed on a disk other than the system disk)

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

Green bamboo biological sprint Hong Kong stocks: loss of more than 500million during the year, tiger medicine and Beijing Yizhuang are shareholders
![[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

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

绿竹生物冲刺港股:年期内亏损超5亿 泰格医药与北京亦庄是股东
随机推荐
ETH数据集下载及相关问题
Flutter: 动作反馈
The impact of telecommuting on all aspects of our experience | community essay solicitation
Eth data set download and related problems
云通信接口更新迭代——SUBMAIL API V4正式上线
visibilitychange – 指定标签页可见时,刷新页面数据
远程办公对我们的各方面影响心得 | 社区征文
Exploration of mobile application performance tools
简单介绍BASE64Encoder的使用
Connect Porsche and 3PL EDI cases
Interpretation of key parameters in MOSFET device manual
helm kubernetes包管理工具
Tech talk activity preview | building intelligent visual products based on Amazon kVs
SSB threshold_ SSB modulation "suggestions collection"
Method of C language self defining function
一年顶十年
[essay solicitation activity] Dear developer, RT thread community calls you to contribute
How to quickly distinguish controlled components from uncontrolled components?
Schoolbag novel multithreaded crawler [easy to understand]
LeetCode:1380. Lucky number in matrix -- simple