-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgroupByCommas.js
More file actions
29 lines (24 loc) · 801 Bytes
/
groupByCommas.js
File metadata and controls
29 lines (24 loc) · 801 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
function groupByCommas(n) {
//convert the input number, n, to a string, then to an array
let result = String(n).split('')
//declare a variable to count every three digits
let countThree = 0
//iterate through the numbers backwards. Every three, add a comma, unless you're at the start of the number
for (let i = result.length - 1; i > 0; i--) {
countThree++
if (countThree === 3) {
countThree = 0
result[i - 1] = result[i - 1] + ',';
}
}
//convert back to a string, then return result
return result.join('')
}
/*
Parameters:
n, an integer number between 0 and 2147483647
Return Value:
The same number, but with commas added every three digits from the right
Example:
420691337 => 420,691,337
*/