Problem overview
The method digitSum receives a non-negative integer n and returns the sum of its digits. For n = 472 the digits are 4, 7 and 2, so the answer is 13. For n = 1005 the answer is 6, because the zeros add nothing, and a one-digit number such as 7 is its own digit sum. For n = 0 the sum is 0.
Approach: take the last digit, then drop it
A number written in base 10 gives up its digits to two simple operations:
n % 10is the remainder after dividing by 10, which is the last digit.472 % 10is 2.- Dividing by 10 and throwing away the fraction removes the last digit.
472 / 10is 47.2, and rounding down gives 47.
Repeat both operations until nothing is left. Each time round the loop adds one digit to a running total and shortens the number by one digit. When n reaches 0, every digit has been added. The digits come off from the right, but a sum doesn't care about order.
Step by step
- Declare
totalwithlet, starting at 0. It changes inside the loop, soconstwould fail. Starting at 0 also gives the right answer forn = 0, where the loop never runs. - Loop while
n > 0. Each pass removes one digit, so the condition becomes false once all digits are gone. - Add
n % 10tototalwith+=. - Replace
nwithMath.floor(n / 10). JavaScript has only one kind of number, so/keeps the fraction:472 / 10is47.2.Math.floorrounds it down to47. Without this line,nnever changes and the loop never ends. - Return
totalafter the loop.
The solution in JavaScript
class Solution {
digitSum(n) {
let total = 0;
while (n > 0) {
total += n % 10;
n = Math.floor(n / 10);
}
return total;
}
}
Here is digitSum(472) pass by pass, with the values after each pass:
| Pass | n | total |
|---|---|---|
| 1 | 47 | 2 |
| 2 | 4 | 9 |
| 3 | 0 | 13 |
After the third pass n is 0, the loop stops, and the method returns 13.
- Parameters can be reassigned. The method changes
ndirectly. That is fine, because numbers are passed by value and the caller's variable is unaffected. - Why
Math.floorand not a cast. JavaScript has no integer division operator.Math.floor(n / 10)is the usual way to get one for non-negative numbers. For negative numbersMath.floorrounds toward negative infinity, but this lesson only has non-negative input.
How many passes
Time: O(d), where d is the number of digits of n, which is about log10(n). The loop runs once per digit. For the largest input, 10^9, that is 10 passes. Space: O(1). Only two variables are used.
Edge cases and common mistakes
- Zero.
digitSum(0)returns 0 because the loop never runs. - Zeros inside the number.
digitSum(1005)adds 5, 0, 0 and 1, and returns 6. - The largest input.
digitSum(1000000000)returns 1. All numbers up to 10^9 are far below the point where JavaScript starts losing integer precision (2^53), so the arithmetic is exact. - Forgetting
Math.floor.n / 10alone leaves fractions behind, sonshrinks as 47.2, 4.72, 0.472 and so on, and never becomes exactly 0 in a useful way. The sum is then wrong or the loop runs for a very long time. - Using
constfortotal. Assigning to aconstvariable throws aTypeErrorat the first+=.
Testing
n = 472gives13.n = 1005gives6.n = 7gives7.n = 0gives0.n = 1000000000gives1.
