Exercise 5 of 10 · Loops
Countdown
What you will make
A rocket countdown that prints every number from 5 down to 1, then announces liftoff.
The one new idea: downto repeats a block once for every number in a falling range
Repeating an action a fixed number of times is one of the most common things a program does. downto is one of several Ruby loops built for exactly that, and it reads almost like the sentence you would use to describe it.
Go straight to the code ↓Repeating with downto
A loop runs the same block of code more than once. downto is one of the plainer ways Ruby writes that: call it on a number, give it a lower number to count down to, and hand it a block wrapped in do ... end. The block runs once for every whole number in between, both ends included, largest first.
downto always includes both numbers you give it. 3.downto(1) visits 3, 2 and 1 — three passes, not two — and the name between the two vertical bars, like |n|, holds whichever of those numbers the current pass is on. Ruby also has upto, which counts the other way, from a smaller number up to a larger one, and times, which just repeats a fixed number of times without needing a starting number at all. All three work the same way once a block is attached: run it once per number, in whatever order that particular method counts.
A worked example
3.downto(1) do |n|
puts "T-minus #{n}"
end
puts "Go!"T-minus 3
T-minus 2
T-minus 1
Go!Three passes through the loop, then the line below it runs once, as ordinary code, the moment the loop finishes. Change the 3 to a 5 and the loop simply runs two passes longer, with no other line needing to change.
Your turn
The editor holds a countdown that is meant to print 5 down to 1, one number per line, then Liftoff!. Press Run first: it stops at 2, and the launch never quite happens, because 1 is missing from the count.
5.downto(2) tells Ruby to stop the moment it reaches 2 — which it does include, but nothing lower. Change the number the loop stops at so the countdown reaches all the way down to 1.
If something goes wrong
If nothing prints inside the loop at all, check that the first number is larger than the second — downto needs to count down, so 5.downto(9) would never run its block even once, the reverse of what upto is for.
If Ruby stops with a syntax error near the end of the file, count the do and end keywords against each other; every do that opens a block needs exactly one end to close it, and a missing one is often reported several lines away from where it actually belongs.
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
- Using upto instead of downto
- 5.upto(1) counts upward from 5, and since 5 is already past 1, the block never runs even once — the loop silently does nothing, with no error to point at it.
- Getting the stopping number wrong by one
- 5.downto(2) stops one number early, and 5.downto(0) runs one extra time and prints a 0 that a real countdown would never show. The stopping number is included in the count, not excluded.
- Printing with + instead of interpolation
- puts "Number: " + i raises TypeError: no implicit conversion of Integer into String, because + joins two strings and refuses to guess whether a number should be turned into text first. puts "Number: #{i}" sidesteps the question entirely.
- Forgetting the matching end for the do block
- Every do started on a downto, times, or each line needs its own end. Miss it and Ruby reports a syntax error, often pointing at the very end of the file rather than the missing line itself.
Want a blank editor instead? Open the Ruby playground.