Skip to content

Exercise 6 of 10 · Loops and conditions

The Clapping Game

What you will make

A counting game from 1 to 10 that claps instead of saying any multiple of three out loud.

The one new idea: an if inside a loop is decided again on every pass

A loop on its own repeats the same action. Putting a condition inside it lets the action change depending on where the loop currently is — the combination behind counting games, score tables, and most of the interesting things a loop ever does.

Go straight to the code ↓

A decision, made again and again

A loop by itself repeats one fixed action. Put an if inside it, and the action stops being fixed — Ruby checks the condition fresh on every single pass, using whatever value the loop is on at that moment, and picks a different branch depending on the answer.

% is the tool for asking "is this a multiple of something?" n % 3 gives the remainder after dividing n by 3, and that remainder is 0 exactly when n divides evenly — in other words, exactly when n is a multiple of 3. Nothing about the loop itself changes; only the number stored in the block variable is different each time round, which is why the same if can produce a different answer on almost every pass.

A worked example

Ruby
(1..6).each do |n|
  if n % 2 == 0
    puts "Beep"
  else
    puts n
  end
end
Output
1
Beep
3
Beep
5
Beep

Six passes through the loop, six separate decisions. The if does not remember what it decided last time — it asks the same question again, about a new number, every single pass.

Your turn

The editor holds a counting game from 1 to 10 that should clap on every multiple of three and say the number everywhere else. Press Run first: it claps on 4 and 8 instead, because the condition checks n % 4 rather than n % 3.

Fix the one number inside the condition so the game claps on 3, 6 and 9, and says every other number plainly. The structure of the if, the loop, and everything else is already correct.

If something goes wrong

If the game claps on the wrong numbers, work out by hand which numbers are being divided by — n % 4 == 0 claps at 4 and 8, n % 3 == 0 claps at 3, 6 and 9 — and compare that against what actually printed.

If Ruby refuses to run at all, count the end keywords: the inner if needs one, and the outer each block needs a second one below it. Two open blocks need two closing ends, in the reverse order they were opened.

Write your code

Runs in your browser. Press Run (or Ctrl/Cmd+Enter) and the output is checked for you.

Ctrl/Cmd+Enter to run

Press Esc then Tab to move keyboard focus out of the code editor.

Ready
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

Writing if n = 3 instead of n % 3 == 0
A single = assigns 3 to n rather than asking a question, and in Ruby the number a variable was just set to counts as true (only nil and false do not), so every single pass would clap, and n itself would be overwritten to 3 on that pass.
Leaving out the matching end for the if
The if inside the loop needs its own end before the loop's own end closes the each block. Miss it and Ruby reports a syntax error that can point at the very last line of the file rather than the missing keyword.
Checking n % 3 == 1 or some other remainder
A remainder of 0 means the division came out exact, which is what makes a number a multiple of 3. Any other target number changes which numbers clap without raising any error, so the only way to catch it is to read the output against what was expected.
Testing n == 3 instead of n % 3 == 0
n == 3 is true for exactly one number in the whole range — 3 itself — so the game would clap once and then never again, missing 6 and 9 entirely.

Want a blank editor instead? Open the Ruby playground.