Skip to content

Commit 9598f12

Browse files
committed
Add more comments
1 parent fd5b533 commit 9598f12

File tree

1 file changed

+10
-8
lines changed

1 file changed

+10
-8
lines changed

sum-of-digits/app.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,13 @@ function calculateSumOfAllDigits(number) {
1919
.split("e")[0]
2020
.replaceAll(".", "")
2121
.replaceAll("-", ""); //remove non digit characters to avoid errors
22-
let sum = 0;
22+
let sum = 0; //initialize sum
2323
for (let i = 0; i < numberString.length; i++) {
24-
const digit = parseInt(numberString[i], 10);
25-
sum += digit;
24+
//repeat for each digit
25+
const digit = parseInt(numberString[i], 10); //convert string to integer
26+
sum += digit; //add digit to sum
2627
}
27-
return sum;
28+
return sum; //return sum of all digits
2829
}
2930

3031
function calculateSumOfIntegerDigits(number) {
@@ -33,12 +34,13 @@ function calculateSumOfIntegerDigits(number) {
3334
.toString()
3435
.replaceAll(".", "")
3536
.replaceAll("-", ""); //remove non digit characters to avoid errors
36-
let sum = 0;
37+
let sum = 0; //initialize sum
3738
for (let i = 0; i < integerString.length; i++) {
38-
const digit = parseInt(integerString[i], 10);
39-
sum += digit;
39+
//repeat for each digit
40+
const digit = parseInt(integerString[i], 10); //convert string to integer
41+
sum += digit; //add digit to sum
4042
}
41-
return sum;
43+
return sum; //return sum of integer digits
4244
}
4345

4446
function calculateSum(number) {

0 commit comments

Comments
 (0)