Skip to content

Added Sum of Digits Implementation #684

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Sep 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 46 additions & 0 deletions Maths/SumOfDigits.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
/*
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 <Array>.reduce
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.
*/
function sumOfDigitsUsingString (number) {
if (number < 0) number = -number

return Number.parseInt(number.toString().split('').reduce((a, b) => Number(a) + Number(b)))
}

/*
The input is divided by 10 in each iteraction, 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 }
16 changes: 16 additions & 0 deletions Maths/test/SumOfDigits.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { sumOfDigitsUsingLoop, sumOfDigitsUsingRecursion, sumOfDigitsUsingString } from '../SumOfDigits'

test('Testing on sumOfDigitsUsingLoop', () => {
const sum = sumOfDigitsUsingLoop(123)
expect(sum).toBe(6)
})

test('Testing on sumOfDigitsUsingRecursion', () => {
const sum = sumOfDigitsUsingRecursion(123)
expect(sum).toBe(6)
})

test('Testing on sumOfDigitsUsingString', () => {
const sum = sumOfDigitsUsingString(123)
expect(sum).toBe(6)
})
Loading