Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create an empty array.
Ruby
arr = [1] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using curly braces {} creates a hash, not an array.
✗ Incorrect
In Ruby, square brackets [] create an empty array.
2fill in blank
mediumComplete the code to create an array with numbers 1 to 5 using a range.
Ruby
arr = (1..[1]).to_a
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 4 excludes the number 5 from the array.
✗ Incorrect
The range 1..5 includes numbers from 1 to 5. Calling to_a converts it to an array.
3fill in blank
hardFix the error in the code to create an array with 3 elements, all set to 0.
Ruby
arr = Array.new([1], 0)
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 0 creates an empty array, not an array with elements.
✗ Incorrect
Array.new(3, 0) creates an array with 3 elements, each set to 0.
4fill in blank
hardFill both blanks to create an array of 4 elements, each initialized to its index multiplied by 2.
Ruby
arr = Array.new([1]) { |[2]| [2] * 2 }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 5 for size creates 5 elements, not 4.
✗ Incorrect
Array.new(4) { |i| i * 2 } creates an array [0, 2, 4, 6].
5fill in blank
hardFill all three blanks to create an array of 3 elements, each a string 'item' followed by its index.
Ruby
arr = Array.new([1]) { |[2]| "[3]#{ [2] }" }
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'index' as string instead of variable name causes errors.
✗ Incorrect
This code creates ["item0", "item1", "item2"] by combining the string 'item' with the index.