diff --git a/Maths/SumOfDigits.js b/Maths/SumOfDigits.js index 9fedf0d2b8..68ae8d6845 100644 --- a/Maths/SumOfDigits.js +++ b/Maths/SumOfDigits.js @@ -1,52 +1,21 @@ -/* - Gets the sum of the digits of the numbers inputted - sumOfDigits(10) will return 1 + 0 = 1 - sumOfDigits(255) will return 2 + 5 + 5 = 12 - Wikipedia: https://en.wikipedia.org/wiki/Digit_sum -*/ - -/* - The given input is converted to a string, split into an array of characters. - This array is reduced to a number using the method .reduce -*/ -function sumOfDigitsUsingString(number) { - if (number < 0) number = -number - - return +number +/** + * @function sumOfDigits + * @description Calculates the sum of all digits of a given integer. + * @param {number} n - An integer + * @returns {number} - The sum of all digits + * @see https://en.wikipedia.org/wiki/Digit_sum + * @example sumOfDigits(123) => 6 + * @example sumOfDigits(9999) => 36 + * @example sumOfDigits(-123) => 6 + */ +const sumOfDigits = (n) => { + if (!Number.isInteger(n)) { + throw new TypeError('Input must be an integer') + } + return Math.abs(n) .toString() .split('') - .reduce((a, b) => +a + +b) + .reduce((sum, digit) => sum + Number(digit), 0) } -/* - The input is divided by 10 in each iteration, till the input is equal to 0 - The sum of all the digits is returned (The res variable acts as a collector, taking the remainders on each iteration) -*/ -function sumOfDigitsUsingLoop(number) { - if (number < 0) number = -number - let res = 0 - - while (number > 0) { - res += number % 10 - number = Math.floor(number / 10) - } - - return res -} - -/* - 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 -*/ -function sumOfDigitsUsingRecursion(number) { - if (number < 0) number = -number - - if (number < 10) return number - - return (number % 10) + sumOfDigitsUsingRecursion(Math.floor(number / 10)) -} - -export { - sumOfDigitsUsingRecursion, - sumOfDigitsUsingLoop, - sumOfDigitsUsingString -} +export { sumOfDigits } diff --git a/Maths/test/SumOfDigits.test.js b/Maths/test/SumOfDigits.test.js index f73891d677..e117bee07e 100644 --- a/Maths/test/SumOfDigits.test.js +++ b/Maths/test/SumOfDigits.test.js @@ -1,20 +1,43 @@ -import { - sumOfDigitsUsingLoop, - sumOfDigitsUsingRecursion, - sumOfDigitsUsingString -} from '../SumOfDigits' - -test('Testing on sumOfDigitsUsingLoop', () => { - const sum = sumOfDigitsUsingLoop(123) - expect(sum).toBe(6) -}) +import { sumOfDigits } from '../SumOfDigits.js' -test('Testing on sumOfDigitsUsingRecursion', () => { - const sum = sumOfDigitsUsingRecursion(123) - expect(sum).toBe(6) -}) +describe('sumOfDigits', () => { + test('sumOfDigits(0) returns 0', () => { + expect(sumOfDigits(0)).toBe(0) + }) + + test('sumOfDigits(1) returns 1', () => { + expect(sumOfDigits(1)).toBe(1) + }) + + test('sumOfDigits(123) returns 6', () => { + expect(sumOfDigits(123)).toBe(6) + }) + + test('sumOfDigits(9999) returns 36', () => { + expect(sumOfDigits(9999)).toBe(36) + }) + + test('sumOfDigits(-123) returns 6', () => { + expect(sumOfDigits(-123)).toBe(6) + }) + + test('sumOfDigits(100) returns 1', () => { + expect(sumOfDigits(100)).toBe(1) + }) + + test('sumOfDigits(999) returns 27', () => { + expect(sumOfDigits(999)).toBe(27) + }) + + test('throws TypeError for float input', () => { + expect(() => sumOfDigits(1.5)).toThrow(TypeError) + }) + + test('throws TypeError for string input', () => { + expect(() => sumOfDigits('123')).toThrow(TypeError) + }) -test('Testing on sumOfDigitsUsingString', () => { - const sum = sumOfDigitsUsingString(123) - expect(sum).toBe(6) + test('throws TypeError for NaN', () => { + expect(() => sumOfDigits(NaN)).toThrow(TypeError) + }) })