当前位置:网站首页>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>
边栏推荐
- ROS知识点——ros::NodeHandle n 和 nh(“~“)的区别
- 海思Hi3798MV100机顶盒芯片介绍[通俗易懂]
- Goodbye, shucang. Alibaba's data Lake construction strategy is really awesome!
- The computer comes with software to make the background color of the picture transparent (matting white background)
- IP地址转换地址段
- 畅玩集团冲刺港股:年营收2.89亿 刘辉有53.46%投票权
- 宝宝巴士创业板IPO被终止:曾拟募资18亿 唐光宇控制47%股权
- Jiuxian's IPO was terminated: Sequoia and Dongfang Fuhai were shareholders who had planned to raise 1billion yuan
- IPtables中SNAT、DNAT和MASQUERADE的含义
- 酒仙网IPO被终止:曾拟募资10亿 红杉与东方富海是股东
猜你喜欢
Win10系统使用pip安装juypter notebook过程记录(安装在系统盘以外的盘)
871. 最低加油次数
AP and F107 data sources and processing
深度之眼(二)——矩阵及其基本运算
The difference of message mechanism between MFC and QT
Tech talk activity preview | building intelligent visual products based on Amazon kVs
七张图,学会做有价值的经营分析
[leetcode] 14. Préfixe public le plus long
Win10 system uses pip to install juypter notebook process record (installed on a disk other than the system disk)
Use of openpose
随机推荐
福元医药上交所上市:市值105亿 胡柏藩身价超40亿
Seven charts, learn to do valuable business analysis
13、Darknet YOLO3
Introduce the scrollintoview() method attribute in detail
从收集到输出:盘点那些强大的知识管理工具——优秀笔记软件盘点(四)
[web technology] 1233 seconds understand web component
Eye of depth (III) -- determinant of matrix
Ap和F107数据来源及处理
Chapter 3 of hands on deep learning - (1) linear regression is realized from scratch_ Learning thinking and exercise answers
JS delete substring in string
綠竹生物沖刺港股:年期內虧損超5億 泰格醫藥與北京亦莊是股東
【Leetcode】13. 罗马数字转整数
Explanation of traceroute command
si446使用记录(一):基本资料获取
酒仙网IPO被终止:曾拟募资10亿 红杉与东方富海是股东
IP address translation address segment
剑指 Offer 26. 树的子结构
The poor family once again gave birth to a noble son: Jiangxi poor county got the provincial number one, what did you do right?
Eth data set download and related problems
chmod命令原理及用法详解[通俗易懂]