Challenge - 5 Problems
Ruby Loop Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of a simple infinite loop with break
What is the output of this Ruby code?
Ruby
count = 0 loop do puts count count += 1 break if count == 3 end
Attempts:
2 left
💡 Hint
The loop stops when count reaches 3, so it prints before breaking.
✗ Incorrect
The loop prints count starting at 0, increments it, and breaks when count equals 3. So it prints 0, 1, 2 and then stops.
❓ Predict Output
intermediate2:00remaining
Loop method with next keyword
What will this Ruby code print?
Ruby
i = 0 loop do i += 1 next if i.even? puts i break if i >= 5 end
Attempts:
2 left
💡 Hint
The next keyword skips even numbers.
✗ Incorrect
The loop increments i, skips printing if i is even, prints odd numbers, and stops when i reaches 5.
🔧 Debug
advanced2:00remaining
Identify the error in this infinite loop
What error does this Ruby code produce?
Ruby
loop do puts "Hello" break if false end
Attempts:
2 left
💡 Hint
The break condition is always false, so the loop never stops.
✗ Incorrect
The break condition is false, so the loop never breaks and prints "Hello" forever without error.
🧠 Conceptual
advanced2:00remaining
Behavior of loop with return inside
What is the value of variable result after running this Ruby code?
Ruby
result = loop do break 42 end
Attempts:
2 left
💡 Hint
The break keyword can return a value from the loop.
✗ Incorrect
In Ruby, break can return a value from the loop, so result gets the value 42.
❓ Predict Output
expert3:00remaining
Output of nested loops with break and next
What is the output of this Ruby code?
Ruby
i = 0 loop do i += 1 j = 0 loop do j += 1 break if j > 2 next if j == 1 puts "i=#{i}, j=#{j}" end break if i >= 2 end
Attempts:
2 left
💡 Hint
The inner loop skips j=1 and breaks after j>2; outer loop runs twice.
✗ Incorrect
For each i (1 and 2), inner loop increments j from 1 to 3. It skips printing when j=1, prints when j=2, then breaks when j>2. So output lines are for j=2 for i=1 and i=2.