Skip to content

Commit b65b685

Browse files
committed
20 - Division Stringified.js
1 parent bab045c commit b65b685

File tree

1 file changed

+43
-0
lines changed

1 file changed

+43
-0
lines changed

20 - Division Stringified.js

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
// Step By Step
2+
3+
function DivisionStringified (num1, num2) {
4+
// First, we use the Math.round method to calculate our product...
5+
// ...and then use the .toString method to convert the reusltant number into a string.
6+
var result = Math.round(num1 / num2).toString();
7+
8+
// Next, we use the .split method to convert our result variable into an array so it can be further manipulated.
9+
// Note that we pass in an empty string ("") into the .split method to split the result variable at every character.
10+
var resultArr = result.split('');
11+
12+
// Next, we check if our result is greater than 1000.
13+
if (result >= 1000) {
14+
// If it is, we loop through each item in our result array...
15+
// ...starting at the third character from the end (i = result.length - 3)...
16+
// ...moving back three charactres each time (i -= 3)...
17+
// ...and stopping when we get to the start of the string (i > 0)
18+
for (var i = result.length - 3; i > 0; i -= 3) {
19+
// At every third character we use the .splice method to insert a comma.
20+
// Note that the first arguement of the .splice is where the method inserts things into the array (i),
21+
// ...the second controls how many array items are removed (0),
22+
// ...and the third controls what is inserted (",").
23+
resultArr.splice(i, 0, ',');
24+
}
25+
}
26+
27+
// Finally, we return our result array with the .join method to covert it into a string.
28+
return resultArr.join('');
29+
}
30+
31+
// No Comments
32+
function DivisionStringified (num1, num2) {
33+
var result = Math.round(num1 / num2).toString();
34+
var resultArr = result.split('');
35+
36+
if (result >= 1000) {
37+
for (var i = result.length - 3; i > 0; i -= 3) {
38+
resultArr.splice(i, 0, ',');
39+
}
40+
}
41+
42+
return resultArr.join('');
43+
}

0 commit comments

Comments
 (0)