×

ruby · level 1 · unit 5 · lesson 3

  1. meet it
  2. guess first
  3. your turn
  4. fix it
  5. nailed it

Tap a green word if you get lost.

meet it

Put an if inside a loop to check every number.

Picture a postie walking down a street. At every door they ask the same question: does this house get a letter? Put an if inside a loop and Ruby can ask a question on every turn too.

asks whether a number is even, and asks whether it is odd. When an if guards one short line, Ruby lets you put it at the end: puts house if house.even?.

To count matches, make a variable set to 0 before the loop. Each time the check is true, add 1 to it. Show it after the loop, once the counting is done.

1.upto(6) do |house|
  if house.even?
    puts "Letter for house #{house}"
  end
end

The if sits inside the block, so it runs once for every house.