Complete the code to print numbers from 1 to 3 using a for loop.
for i in [1] puts i end
The for loop iterates over the range 1..3, which includes 1, 2, and 3.
Complete the code to print each fruit in the array using a for loop.
fruits = ["apple", "banana", "cherry"] for [1] in fruits puts fruit end
fruits as the loop variable.The variable fruit is used to represent each element in the fruits array during the loop.
Fix the error in the for loop to correctly print numbers from 1 to 5.
for i in [1] puts i end
1...5 which excludes 5.range which is invalid in Ruby.The range 1..5 includes the number 5, so the loop prints 1 through 5.
Fill both blanks to create a for loop that prints only even numbers from 2 to 10.
for num in [1] puts num if num [2] 0 end
% 2 != 0 which checks for odd numbers.The loop iterates from 2 to 10. The condition num % 2 == 0 checks if the number is even before printing.
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.
for [1] in [2] square = [1] * [1] puts square if square > 10 end
The loop variable n goes from 1 to 5. The square is calculated by multiplying n by itself. Only squares greater than 10 are printed.