0
0
Rubyprogramming~10 mins

Break, next, and redo behavior in Ruby - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete 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'
Anext
Bbreak
Credo
Dreturn
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.
2fill in blank
medium

Complete 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'
Anext
Bredo
Cbreak
Dexit
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'break' which stops the entire loop.
Using 'redo' which repeats the current iteration endlessly.
3fill in blank
hard

Fix 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'
Abreak
Bretry
Cnext
Dredo
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'next' which skips to the next iteration.
Using 'retry' which is not valid inside this loop context.
4fill in blank
hard

Fill 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'
Anext
Bbreak
Credo
Dreturn
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'break' to skip 2 which stops the whole loop.
Using 'redo' which repeats the iteration endlessly.
5fill in blank
hard

Fill 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'
Aredo
Bnext
Cbreak
Dreturn
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'next' instead of 'redo' for repeating.
Using 'break' too early stopping the loop before intended.