Files
JavaScript/Maths/SumOfDigits.js
Roland Hummel 86d333ee94 feat: Test running overhaul, switch to Prettier & reformat everything (#1407)
* chore: Switch to Node 20 + Vitest

* chore: migrate to vitest mock functions

* chore: code style (switch to prettier)

* test: re-enable long-running test

Seems the switch to Node 20 and Vitest has vastly improved the code's and / or the test's runtime!

see #1193

* chore: code style

* chore: fix failing tests

* Updated Documentation in README.md

* Update contribution guidelines to state usage of Prettier

* fix: set prettier printWidth back to 80

* chore: apply updated code style automatically

* fix: set prettier line endings to lf again

* chore: apply updated code style automatically

---------

Co-authored-by: github-actions <${GITHUB_ACTOR}@users.noreply.github.com>
Co-authored-by: Lars Müller <34514239+appgurueu@users.noreply.github.com>
2023-10-04 02:38:19 +05:30

53 lines
1.4 KiB
JavaScript

/*
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
*/
function sumOfDigitsUsingString(number) {
if (number < 0) number = -number
return +number
.toString()
.split('')
.reduce((a, b) => +a + +b)
}
/*
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
}