Skip to content

Commit c525e43

Browse files
feat: add sum of digits algorithm
1 parent 5c39e87 commit c525e43

2 files changed

Lines changed: 57 additions & 65 deletions

File tree

‎Maths/SumOfDigits.js‎

Lines changed: 17 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,21 @@
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-
*/
12-
function sumOfDigitsUsingString(number) {
13-
if (number < 0) number = -number
14-
15-
return +number
1+
/**
2+
* @function sumOfDigits
3+
* @description Calculates the sum of all digits of a given integer.
4+
* @param {number} n - An integer
5+
* @returns {number} - The sum of all digits
6+
* @see https://en.wikipedia.org/wiki/Digit_sum
7+
* @example sumOfDigits(123) => 6
8+
* @example sumOfDigits(9999) => 36
9+
* @example sumOfDigits(-123) => 6
10+
*/
11+
const sumOfDigits = (n) => {
12+
if (!Number.isInteger(n)) {
13+
throw new TypeError('Input must be an integer')
14+
}
15+
return Math.abs(n)
1616
.toString()
1717
.split('')
18-
.reduce((a, b) => +a + +b)
18+
.reduce((sum, digit) => sum + Number(digit), 0)
1919
}
2020

21-
/*
22-
The input is divided by 10 in each iteration, till the input is equal to 0
23-
The sum of all the digits is returned (The res variable acts as a collector, taking the remainders on each iteration)
24-
*/
25-
function sumOfDigitsUsingLoop(number) {
26-
if (number < 0) number = -number
27-
let res = 0
28-
29-
while (number > 0) {
30-
res += number % 10
31-
number = Math.floor(number / 10)
32-
}
33-
34-
return res
35-
}
36-
37-
/*
38-
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
39-
*/
40-
function sumOfDigitsUsingRecursion(number) {
41-
if (number < 0) number = -number
42-
43-
if (number < 10) return number
44-
45-
return (number % 10) + sumOfDigitsUsingRecursion(Math.floor(number / 10))
46-
}
47-
48-
export {
49-
sumOfDigitsUsingRecursion,
50-
sumOfDigitsUsingLoop,
51-
sumOfDigitsUsingString
52-
}
21+
export { sumOfDigits }

‎Maths/test/SumOfDigits.test.js‎

Lines changed: 40 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,43 @@
1-
import {
2-
sumOfDigitsUsingLoop,
3-
sumOfDigitsUsingRecursion,
4-
sumOfDigitsUsingString
5-
} from '../SumOfDigits'
6-
7-
test('Testing on sumOfDigitsUsingLoop', () => {
8-
const sum = sumOfDigitsUsingLoop(123)
9-
expect(sum).toBe(6)
10-
})
1+
import { sumOfDigits } from '../SumOfDigits.js'
112

12-
test('Testing on sumOfDigitsUsingRecursion', () => {
13-
const sum = sumOfDigitsUsingRecursion(123)
14-
expect(sum).toBe(6)
15-
})
3+
describe('sumOfDigits', () => {
4+
test('sumOfDigits(0) returns 0', () => {
5+
expect(sumOfDigits(0)).toBe(0)
6+
})
7+
8+
test('sumOfDigits(1) returns 1', () => {
9+
expect(sumOfDigits(1)).toBe(1)
10+
})
11+
12+
test('sumOfDigits(123) returns 6', () => {
13+
expect(sumOfDigits(123)).toBe(6)
14+
})
15+
16+
test('sumOfDigits(9999) returns 36', () => {
17+
expect(sumOfDigits(9999)).toBe(36)
18+
})
19+
20+
test('sumOfDigits(-123) returns 6', () => {
21+
expect(sumOfDigits(-123)).toBe(6)
22+
})
23+
24+
test('sumOfDigits(100) returns 1', () => {
25+
expect(sumOfDigits(100)).toBe(1)
26+
})
27+
28+
test('sumOfDigits(999) returns 27', () => {
29+
expect(sumOfDigits(999)).toBe(27)
30+
})
31+
32+
test('throws TypeError for float input', () => {
33+
expect(() => sumOfDigits(1.5)).toThrow(TypeError)
34+
})
35+
36+
test('throws TypeError for string input', () => {
37+
expect(() => sumOfDigits('123')).toThrow(TypeError)
38+
})
1639

17-
test('Testing on sumOfDigitsUsingString', () => {
18-
const sum = sumOfDigitsUsingString(123)
19-
expect(sum).toBe(6)
40+
test('throws TypeError for NaN', () => {
41+
expect(() => sumOfDigits(NaN)).toThrow(TypeError)
42+
})
2043
})

0 commit comments

Comments
 (0)