Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Challenge: Finding the Sum Threshold | Discovering Loops
Introduction to JavaScript
Section 6. Chapter 4
single

single

Challenge: Finding the Sum Threshold

Swipe to show menu

Task

Swipe to start coding

Find out how many consecutive numbers (starting from 1) must be added together before their running total exceeds a given limit.

For example, if

limit = 15
1 + 2 + 3 + 4 + 5 = 15 → not yet over
1 + 2 + 3 + 4 + 5 + 6 = 21 → now it's over 15

So the answer would be 6 numbers.

  1. Use a while loop to keep adding numbers until the condition is met.

  2. Inside the loop, on every iteration:

    • Increment count by 1.
    • Add the current value of count to sum.
    • The loop should keep running as long as sum is less than or equal to limit.
  3. Once the loop ends, return count, the number of terms it took to exceed the limit.

Solution

Switch to desktopSwitch to desktop for real-world practiceContinue from where you are using one of the options below
Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 6. Chapter 4
single

single

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

some-alt