Start learning free

LESSON 216 OF 671 · WHILE LOOPS - CHALLENGES

Sum Until Limit in JavaScript: A while Loop That Stops at a Total

Practice Read the full explanation

Given a positive integer limit, add up the numbers 1, 2, 3, … in this order, and stop as soon as the total is at least limit. Return how many numbers you added.

The total may go past limit: the number that makes it reach or pass limit is the last one added, and it counts.

 

Example 1:

Input: limit = 10
Output: 4
Explanation: 1 + 2 + 3 + 4 = 10 reaches the limit exactly, so 4 numbers were added.

Example 2:

Input: limit = 11
Output: 5
Explanation: 1 + 2 + 3 + 4 = 10 is still less than 11, so 5 is added too. The total becomes 15, and 5 numbers were added.

Example 3:

Input: limit = 1
Output: 1
Explanation: The first number, 1, already reaches the limit.

 

Constraints:

  • 1 <= limit <= 109
STEP 1 OF 6
    Ctrl + Enter to check

    JAVASCRIPT · WHILE LOOPS - CHALLENGES

    Sum Until Limit in JavaScript, explained

    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

    1. Declare total with let, starting at 0. It holds the sum so far.
    2. Declare count with let, starting at 0. It counts the numbers added.
    3. Loop while total < limit. The loop stops when the total is at least the limit, which is exactly when adding must stop.
    4. Increase count by 1 with ++. It is now the next number to add: 1 on the first pass, 2 on the second, and so on.
    5. Add count to total. The order of these two lines matters, because count has to grow before it is added.
    6. Return count after 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:

    Passcounttotal
    111
    223
    336
    4410
    5515

    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 when total >= limit, which includes the case where the total equals the limit. With <= instead, limit = 10 would 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 += count comes 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. total and count are reassigned, so they need let.

    Testing

    • limit = 10 gives 4.
    • limit = 11 gives 5.
    • limit = 1 gives 1.
    • limit = 2 gives 2.
    • limit = 1000000000 gives 44721.
    Keep going with JavaScript650+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free