Challenge - 5 Problems
Unless Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of unless with negated condition
What is the output of this Ruby code snippet?
Ruby
x = 5 unless x > 10 puts "Hello" else puts "Goodbye" end
Attempts:
2 left
💡 Hint
Remember that 'unless' runs the block only if the condition is false.
✗ Incorrect
Since x is 5, the condition 'x > 10' is false. 'unless' runs the block when condition is false, so it prints 'Hello'.
❓ Predict Output
intermediate2:00remaining
Using unless with else clause
What will this Ruby code output?
Ruby
score = 85 unless score < 60 puts "Pass" else puts "Fail" end
Attempts:
2 left
💡 Hint
Think about when 'unless' executes the main block versus the else block.
✗ Incorrect
The condition 'score < 60' is false because score is 85. 'unless' runs the main block when condition is false, so it prints 'Pass'.
🔧 Debug
advanced2:00remaining
Identify the error in unless usage
What error does this Ruby code produce?
Ruby
value = 10 unless value > 5 puts "Low" puts "Check" else puts "High" end
Attempts:
2 left
💡 Hint
Check if all blocks are properly closed with 'end'.
✗ Incorrect
The 'unless' block is missing its closing 'end', causing a SyntaxError.
❓ Predict Output
advanced2:00remaining
Output of nested unless statements
What is the output of this Ruby code?
Ruby
a = false b = true unless a unless b puts "No" else puts "Yes" end end
Attempts:
2 left
💡 Hint
Evaluate the outer 'unless' first, then the inner one.
✗ Incorrect
Outer 'unless a' runs because a is false. Inner 'unless b' does not run because b is true, so else prints 'Yes'.
🧠 Conceptual
expert2:00remaining
Understanding unless with negated conditions
Which statement best describes the use of 'unless' with negated conditions in Ruby?
Attempts:
2 left
💡 Hint
Think about how 'unless' reads compared to 'if' with negation.
✗ Incorrect
'unless' is best used for negated conditions to make code easier to read, avoiding double negatives.