Challenge - 5 Problems
Times Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of Times with Block Variable
What is the output of this Ruby code?
Ruby
3.times do |i| puts i * 2 end
Attempts:
2 left
💡 Hint
Remember, the block variable starts at 0 and goes up to one less than the count.
✗ Incorrect
The times method runs the block 3 times with i values 0, 1, and 2. Multiplying each by 2 gives 0, 2, and 4.
❓ Predict Output
intermediate2:00remaining
Times Method Without Block Variable
What will this Ruby code print?
Ruby
5.times { puts "Hello" }
Attempts:
2 left
💡 Hint
If no block variable is given, the block still runs the specified number of times.
✗ Incorrect
The times method runs the block 5 times, printing "Hello" each time.
🔧 Debug
advanced2:00remaining
Identify the Error in Times Usage
What error does this Ruby code produce?
Ruby
4.times do puts i end
Attempts:
2 left
💡 Hint
Check if the block variable is defined or accessible inside the block.
✗ Incorrect
The block does not define a variable i, so trying to puts i raises a NameError.
❓ Predict Output
advanced2:00remaining
Times Method with Return Value
What is the value of variable result after running this Ruby code?
Ruby
result = 3.times.map { |i| i * 3 }
Attempts:
2 left
💡 Hint
Check the return type of times and whether it supports map.
✗ Incorrect
The times method returns the original integer, which does not have a map method, causing a NoMethodError.
❓ Predict Output
expert2:00remaining
Complex Times Loop with Conditional Output
What is the output of this Ruby code?
Ruby
5.times do |i| if i.even? print i else print "-" end end
Attempts:
2 left
💡 Hint
Check how even numbers and odd numbers are printed inside the loop.
✗ Incorrect
For i from 0 to 4, even numbers print the number, odd numbers print a dash, resulting in '0-2-4'.