Exercise 5 of 10 · Loops
Countdown
What you will make
A launch sequence that counts itself down from five to one, announces liftoff and draws a small rocket, with the whole countdown written as one line that runs five times.
The one new idea: a for loop repeats an action
Repeating work is most of what a computer is for: every row of a table, every message in a mailbox, every frame of an animation. A for loop is how JavaScript asks for that, and the three parts you write here are the same three parts in almost every language you will meet afterwards.
Go straight to the code ↓Writing a line once and running it many times
A countdown is one sentence said over and over with a different number in it. You could write five separate
console.log lines by hand, but five is the gentle case: a program greeting a thousand people cannot be
typed out.
A for loop is how you write the line once and say how many times to run it.
for (let n = 1; n <= 4; n++) {
// the lines in here run four times
}Everything the loop needs sits between the round brackets, in three parts separated by semicolons. Read them as three questions.
- Where does it start?
let n = 1makes a counter callednand puts 1 in it, once, before the first pass. - How long does it keep going?
n <= 4is checked before every pass. True and the block runs again; false and the loop is over and JavaScript carries on below it. - What changes each time?
n++happens at the end of every pass. Two plus signs mean add one; two minus signs mean take one away.
After the brackets come curly braces, doing the job they did for if: the lines inside run once per pass,
and the lines after the closing brace run once, when the counting is done.
Why this one cannot be const
Every name you have made so far has been a const, which keeps its value for good.
A counter is the opposite: its entire purpose is to hold a different number on every pass. Write
for (const n = 1; n <= 4; n++) and the block runs once, then the program stops with
TypeError: Assignment to constant variable. — n++ was told to change something that had been fixed.
let is the other way to name a value, and this is what it is for: a name meant to move. Reach for const
everywhere else, so let stays a signal that this one changes.
A worked example
Seats rather than a rocket, so the countdown stays yours to write:
for (let seat = 1; seat <= 3; seat++) {
console.log("Seat", seat, "ready");
}
console.log("All seats checked.");Seat 1 ready
Seat 2 ready
Seat 3 ready
All seats checked.Three passes, and seat holds a different number on each one without you ever setting it again. The last
line sits outside the braces, so it runs once, after the loop has finished.
Your turn
The editor holds a launch sequence that already runs. Press Run before changing anything and the trouble shows itself: the count reads 0, 1, 2 and then announces liftoff. No rocket has ever left the ground that way.
One line needs rewriting, the one beginning with for:
for (let i = 0; i <= 2; i++) {The banners, the rocket and the printing line between the braces are finished — leave them alone.
Rewrite the three parts so the count reads 5, 4, 3, 2, 1 and then hands over to the liftoff banner. Start at five, keep going while the counter has not yet dropped below one, and take one away each pass instead of adding one.
If something goes wrong
A countdown one number too long or too short is the likeliest result, and it is usually the middle part. The
test is asked before each pass, so the last number printed is the last one for which it was still true. Let
the test survive zero and a T minus 0 line sneaks in before the banner. If the count starts on the wrong
number instead, it is the first part rather than the test.
The next likeliest is a loop that never finishes, which happens when the counter travels away from the finish rather than towards it. The page notices, stops the run and says so. Nothing is damaged; the step simply needs to move the counter the way the test is waiting for.
And if nothing appears between the two banners, the loop took no passes: the test was already false on the very first check. Read the three parts left to right as start, keep going, change, then run it again.
Write your code
Runs in your browser. Press Run (or Ctrl/Cmd+Enter) and the output is checked for you.
Press Esc then Tab to move keyboard focus out of the code editor.
Output will appear here after you run your code.The runtime is starting in the background. You can type now — it will be ready before you are.
The answer appears here once you have run your code at least once.
Things that often go wrong here
- Naming the counter with const instead of let
- The first line of the countdown prints, and then the program stops with TypeError: Assignment to constant variable. A const name keeps its value for good, while a counter has to be a different number on every pass — which is the one job let exists for.
- Separating the three parts with commas instead of semicolons
- JavaScript reads it as naming several things at once rather than as the three parts of a loop, and stops before running a single line with SyntaxError: Identifier i has already been declared. Not even the banners appear, and that empty output is the tell that nothing ran at all.
- Counting upwards while the test waits for the counter to come down
- The counter moves away from the finish instead of towards it, so the test never turns false and the loop never ends. The page notices and stops the run for you, so nothing is damaged, but liftoff never arrives.
- Letting the test stay true one number too long
- A test that is still true at zero prints a T minus 0 line before liftoff. The test is asked once before every pass, so the last number you see is the last one for which it was still true.
Want a blank editor instead? Open the JavaScript playground.