Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Sum with a for Loop

Fundamentals: Part 2easy

Course · Section 3: JavaScript Fundamentals – Part 2 · Lecture 47: Iteration: The for Loop

Implement `sumLoop(n)` returning `1 + 2 + ... + n` using a for loop.

Sample tests

Input: sumLoop(5)
Output: 15
Input: sumLoop(1)
Output: 1

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • An off-by-one loop bound missing n or starting at 0.
Visualize this concept: let, const, var, and the Temporal Dead Zone →
Approach & explanation (try first)

A for loop adds each integer from 1 to n into a running total.

Loading...
⌘/Ctrl + Enter

Run your code to see results.