0
0
Rubyprogramming~10 mins

For loop (rarely used 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 print numbers from 1 to 3 using a for loop.

Ruby
for i in [1]
  puts i
end
Drag options to blanks, or click blank then click option'
Arange(1,3)
B[1, 2, 3, 4]
C1...3
D1..3
Attempts:
3 left
💡 Hint
Common Mistakes
Using Python-style range function which doesn't exist in Ruby.
Using exclusive range 1...3 which excludes 3.
2fill in blank
medium

Complete the code to print each fruit in the array using a for loop.

Ruby
fruits = ["apple", "banana", "cherry"]
for [1] in fruits
  puts fruit
end
Drag options to blanks, or click blank then click option'
Afruit
Bitem
Cf
Dfruits
Attempts:
3 left
💡 Hint
Common Mistakes
Using the array name fruits as the loop variable.
Using a variable name that is not used inside the loop.
3fill in blank
hard

Fix the error in the for loop to correctly print numbers from 1 to 5.

Ruby
for i in [1]
  puts i
end
Drag options to blanks, or click blank then click option'
A1...5
B1..5
Crange(1, 5)
D[1, 2, 3, 4]
Attempts:
3 left
💡 Hint
Common Mistakes
Using exclusive range 1...5 which excludes 5.
Using Python-style range which is invalid in Ruby.
4fill in blank
hard

Fill both blanks to create a for loop that prints only even numbers from 2 to 10.

Ruby
for num in [1]
  puts num if num [2] 0
end
Drag options to blanks, or click blank then click option'
A2..10
B% 2 ==
C% 2 !=
D1..10
Attempts:
3 left
💡 Hint
Common Mistakes
Using the wrong range that starts at 1.
Using % 2 != 0 which checks for odd numbers.
5fill in blank
hard

Fill all three blanks to create a for loop that prints squares of numbers from 1 to 5 only if the square is greater than 10.

Ruby
for [1] in [2]
  square = [1] * [1]
  puts square if square > 10
end
Drag options to blanks, or click blank then click option'
An
B1..5
Di
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently.
Using a range that does not include 5.