← Back to challenges

Sum of Digits

JavaScriptHardnumbersmathalgebrarecursion

Instructions

Create a function that takes a range of numbers and returns the sum of each digit from start to stop.

Examples

digitsSum(1, 10) ➞ 46
// total numbers in the range are = 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
// sum of each digits is = 1 + 2 + 3 + 4 + 5 + 6 + 7 + 8 + 9 + 1 + 0 = 46

digitsSum(1, 20) ➞ 102

digitsSum(1, 100) ➞ 901

Notes

start and stop are inclusive in the range.

javascript
Loading editor…
to run
Walks through the solution with reasoning and edge cases.