Start learning free

LESSON 207 OF 505 · WHILE LOOPS - CHALLENGES

Sum Until Limit in TypeScript

Practice

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

    Sum Until Limit in TypeScript

    Keep going with TypeScript500+ step-by-step lessons, from your first line of code to coding interviews. Free to start.
    Start learning free