当前位置:网站首页>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>
边栏推荐
- A few lines of code to complete RPC service registration and discovery
- Listing of chaozhuo Aviation Technology Co., Ltd.: raising 900million yuan, with a market value of more than 6billion yuan, becoming the first science and technology innovation board enterprise in Xia
- The poor family once again gave birth to a noble son: Jiangxi poor county got the provincial number one, what did you do right?
- Meanings of SNAT, DNAT and masquerade in iptables
- Exploration of mobile application performance tools
- 剑指 Offer 27. 二叉树的镜像
- Eye of depth (II) -- matrix and its basic operations
- Seven charts, learn to do valuable business analysis
- Qwebengineview crash and alternatives
- Idea2021.1 installation tutorial
猜你喜欢
ETH数据集下载及相关问题
Qstype implementation of self drawing interface project practice (II)
Error when uploading code to remote warehouse: remote origin already exists
TCP拥塞控制详解 | 2. 背景
The poor family once again gave birth to a noble son: Jiangxi poor county got the provincial number one, what did you do right?
剑指 Offer 27. 二叉树的镜像
【Leetcode】13. 罗马数字转整数
Listing of chaozhuo Aviation Technology Co., Ltd.: raising 900million yuan, with a market value of more than 6billion yuan, becoming the first science and technology innovation board enterprise in Xia
Sword finger offer 24 Reverse linked list
ThreadLocal
随机推荐
【Leetcode】13. Roman numeral to integer
【Leetcode】14. 最長公共前綴
Baobab's gem IPO was terminated: Tang Guangyu once planned to raise 1.8 billion to control 47% of the equity
博客主题 “Text“ 夏日清新特别版
社交元宇宙平台Soul冲刺港股:年营收12.8亿 腾讯是股东
Win10 system uses pip to install juypter notebook process record (installed on a disk other than the system disk)
QWebEngineView崩溃及替代方案
&lt; IV & gt; H264 decode output YUV file
默认浏览器设置不了怎么办?
Qwebengineview crash and alternatives
ROS知识点——消息过滤器 ( message_filters)
PCL知识点——体素化网格方法对点云进行下采样
VScode知识点——常见报错
visibilitychange – 指定标签页可见时,刷新页面数据
A few lines of code to complete RPC service registration and discovery
福元医药上交所上市:市值105亿 胡柏藩身价超40亿
JS delete substring in string
Vscode setting delete line shortcut [easy to understand]
871. Minimum refueling times
求简单微分方程