Skip to content

Commit a7216d4

Browse files
authored
Create sum_of_digits.js
1 parent d44924a commit a7216d4

File tree

1 file changed

+21
-0
lines changed

1 file changed

+21
-0
lines changed

algorithms/sum_of_digits.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
3+
Given an integer, return the sum of all the individual digits in that integer
4+
input: 4321
5+
output: 4+3+2+1 = 10
6+
7+
*/
8+
9+
10+
function sumDigit(n){
11+
12+
if (n.toString().length <= 1) return n;
13+
14+
return (n % 10) + sumDigit( Math.floor(n/10) );
15+
}
16+
17+
18+
sumDigit(4321);
19+
20+
21+

0 commit comments

Comments
 (0)