Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to exit the loop when the number is 5.
Ruby
numbers = [1, 2, 3, 4, 5, 6] numbers.each do |num| if num == 5 [1] end puts num end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'next' instead of 'break' which skips to the next iteration.
Using 'redo' which repeats the current iteration endlessly.
✗ Incorrect
The break keyword exits the loop immediately when the condition is met.
2fill in blank
mediumComplete the code to skip printing the number 3 but continue the loop.
Ruby
numbers = [1, 2, 3, 4, 5] numbers.each do |num| if num == 3 [1] end puts num end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'break' which stops the entire loop.
Using 'redo' which repeats the current iteration endlessly.
✗ Incorrect
The next keyword skips the current iteration and continues with the next one.
3fill in blank
hardFix the error in the code to repeat the current iteration when the number is even.
Ruby
numbers = [1, 2, 3] numbers.each do |num| if num.even? [1] end puts num end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'next' which skips to the next iteration.
Using 'retry' which is not valid inside this loop context.
✗ Incorrect
The redo keyword repeats the current iteration without moving to the next element.
4fill in blank
hardFill both blanks to skip printing 2 and stop the loop at 4.
Ruby
numbers = [1, 2, 3, 4, 5] numbers.each do |num| if num == 2 [1] elsif num == 4 [2] end puts num end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'break' to skip 2 which stops the whole loop.
Using 'redo' which repeats the iteration endlessly.
✗ Incorrect
Use next to skip printing 2 and break to stop the loop at 4.
5fill in blank
hardFill all three blanks to repeat the iteration for odd numbers, skip 4, and stop at 5.
Ruby
numbers = [1, 2, 3, 4, 5] numbers.each do |num| if num.odd? [1] elsif num == 4 [2] elsif num == 5 [3] end puts num end
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'next' instead of 'redo' for repeating.
Using 'break' too early stopping the loop before intended.
✗ Incorrect
Use redo to repeat the iteration for odd numbers, next to skip 4, and break to stop at 5.