Skip to content

Commit b3b4ad4

Browse files
SpiderMathSpiderMath
and
SpiderMath
authored
chore: Added Sum of Digits Implementation (TheAlgorithms#684)
* Added the main logic, need to work on Tests * Added tests for SOD * Fix typo and add Wikipedia link in comments * Fix mistake in SumOfDigitsUsingStrings I intended to initially write a different implementation but I wrote something else 🤦‍♂️ * Converted Spacing from Tabs to Spaces * Oops, forgot about the test file * Fixed semicolon problems... * Oops, I missed a few semicolons * Linting is hell TwT Co-authored-by: SpiderMath <{ID}+{username}@users.noreply.github.com>
1 parent 65ec706 commit b3b4ad4

File tree

3 files changed

+12488
-7
lines changed

3 files changed

+12488
-7
lines changed

Maths/SumOfDigits.js

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
/*
2+
Gets the sum of the digits of the numbers inputted
3+
sumOfDigits(10) will return 1 + 0 = 1
4+
sumOfDigits(255) will return 2 + 5 + 5 = 12
5+
Wikipedia: https://en.wikipedia.org/wiki/Digit_sum
6+
*/
7+
8+
/*
9+
The given input is converted to a string, split into an array of characters.
10+
This array is reduced to a number using the method <Array>.reduce
11+
NOTE: The final parseInt is just there in cases where 1 digit numbers are given, since without that it would result in a String output.
12+
*/
13+
function sumOfDigitsUsingString (number) {
14+
if (number < 0) number = -number
15+
16+
return Number.parseInt(number.toString().split('').reduce((a, b) => Number(a) + Number(b)))
17+
}
18+
19+
/*
20+
The input is divided by 10 in each iteraction, till the input is equal to 0
21+
The sum of all the digits is returned (The res variable acts as a collector, taking the remainders on each iteration)
22+
*/
23+
function sumOfDigitsUsingLoop (number) {
24+
if (number < 0) number = -number
25+
let res = 0
26+
27+
while (number > 0) {
28+
res += number % 10
29+
number = Math.floor(number / 10)
30+
}
31+
32+
return res
33+
}
34+
35+
/*
36+
We use the fact that the sum of the digits of a one digit number is itself, and check whether the number is less than 10. If so, then we return the number. Else, we take the number divided by 10 and floored, and recursively call the function, while adding it with the number mod 10
37+
*/
38+
function sumOfDigitsUsingRecursion (number) {
39+
if (number < 0) number = -number
40+
41+
if (number < 10) return number
42+
43+
return (number % 10) + sumOfDigitsUsingRecursion(Math.floor(number / 10))
44+
}
45+
46+
export { sumOfDigitsUsingRecursion, sumOfDigitsUsingLoop, sumOfDigitsUsingString }

Maths/test/SumOfDigits.test.js

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import { sumOfDigitsUsingLoop, sumOfDigitsUsingRecursion, sumOfDigitsUsingString } from '../SumOfDigits'
2+
3+
test('Testing on sumOfDigitsUsingLoop', () => {
4+
const sum = sumOfDigitsUsingLoop(123)
5+
expect(sum).toBe(6)
6+
})
7+
8+
test('Testing on sumOfDigitsUsingRecursion', () => {
9+
const sum = sumOfDigitsUsingRecursion(123)
10+
expect(sum).toBe(6)
11+
})
12+
13+
test('Testing on sumOfDigitsUsingString', () => {
14+
const sum = sumOfDigitsUsingString(123)
15+
expect(sum).toBe(6)
16+
})

0 commit comments

Comments
 (0)