Problem overview
The method sumUntilLimit receives a positive integer limit. It adds the numbers 1, 2, 3 and so on, in order, and stops as soon as the total is at least limit. It returns how many numbers it added. The last number counts even when the total goes past the limit.
For limit = 10, 1 + 2 + 3 + 4 reaches exactly 10 after four numbers, so the answer is 4. For limit = 11, the total after four numbers is 10, which is still below 11, so 5 is added as well. The total becomes 15 and the answer is 5. For limit = 1 the first number already reaches the limit, so the answer is 1.
Approach: two variables and one loop
You can't know how many numbers will be needed, so a while loop that checks the total before each pass is the natural fit. Two variables are enough:
total, the sum of the numbers added so far;count, how many numbers have been added.
Because the numbers go 1, 2, 3 in order, count is also the last number added. So count doubles as the next number to add, and no third variable is needed.
Step by step
- Declare
totalwithlet, starting at 0. It holds the sum so far. - Declare
countwithlet, starting at 0. It counts the numbers added. - Loop while
total < limit. The loop stops when the total is at least the limit, which is exactly when adding must stop. - Increase
countby 1 with++. It is now the next number to add: 1 on the first pass, 2 on the second, and so on. - Add
counttototal. The order of these two lines matters, becausecounthas to grow before it is added. - Return
countafter the loop.
The solution in JavaScript
class Solution {
sumUntilLimit(limit) {
let total = 0;
let count = 0;
while (total < limit) {
count++;
total += count;
}
return count;
}
}
For sumUntilLimit(11), the values after each pass are:
| Pass | count | total |
|---|---|---|
| 1 | 1 | 1 |
| 2 | 2 | 3 |
| 3 | 3 | 6 |
| 4 | 4 | 10 |
| 5 | 5 | 15 |
After pass 4 the total is 10, which is below 11, so the loop runs again. After pass 5 the total is 15, the condition 15 < 11 is false, and the method returns 5.
- The condition is checked before every pass. The check happens at the top of the loop, never in the middle. That is why the total can end above the limit: the pass that crosses the limit finishes completely, and only then does the check stop the loop.
- Strict against non-strict comparison. The loop continues while
total < limit, so it stops whentotal >= limit, which includes the case where the total equals the limit. With<=instead,limit = 10would add a fifth number.
Edge cases and common mistakes
- Limit 1.
sumUntilLimit(1)returns 1: the first pass gives a total of 1, which reaches the limit. - Small limits.
sumUntilLimit(2)returns 2, because the first total, 1, is below 2 and the second, 3, is not.sumUntilLimit(3)also returns 2. - The largest input.
sumUntilLimit(1000000000)returns 44721. The total grows faster and faster, so even for a limit of a billion the loop needs only about forty-five thousand passes. - Adding before incrementing. If
total += countcomes first, the first number added is 0, and the numbers are off by one: the sums run 0, 1, 3, 6, and the method returns a count that is too high. - Returning
total. The question asks how many numbers were added, not what the sum is. - Using
const.totalandcountare reassigned, so they needlet.
Testing
limit = 10gives4.limit = 11gives5.limit = 1gives1.limit = 2gives2.limit = 1000000000gives44721.
