Practice
JavaScriptData StructuresReactConcepts
Sign in
← Back to problems

Countdown with while

Fundamentals: Part 2easy

Course · Section 3: JavaScript Fundamentals – Part 2 · Lecture 50: The while Loop

Implement `whileCountdown(n)` returning `[n, n-1, ..., 1]` using a while loop.

Sample tests

Input: whileCountdown(3)
Output: [3,2,1]
Input: whileCountdown(0)
Output: []

+ 1 hidden test run on Submit.

Hints

Common pitfalls
  • Forgetting to decrement creates an infinite loop.
Approach & explanation (try first)

A while loop pushes and decrements until the counter drops below 1.

Loading...
⌘/Ctrl + Enter

Run your code to see results.