Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to create an empty array in Ruby.
Ruby
my_array = [1] Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using curly braces {} which create a hash, not an array.
Using parentheses () which are for method calls or grouping.
✗ Incorrect
In Ruby, arrays are created using square brackets [].
2fill in blank
mediumComplete the code to add the number 5 to the end of the array.
Ruby
numbers = [1, 2, 3] numbers.[1](5) puts numbers.inspect
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop which removes the last element.
Using shift which removes the first element.
✗ Incorrect
The push method adds an element to the end of an array.
3fill in blank
hardFix the error in the code to access the first element of the array.
Ruby
fruits = ['apple', 'banana', 'cherry'] first = fruits[1] puts first
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using parentheses instead of square brackets.
Using curly braces which are for hashes.
✗ Incorrect
Arrays in Ruby use square brackets with zero-based indexes to access elements.
4fill in blank
hardFill both blanks to create an array of squares for numbers 1 to 5.
Ruby
squares = (1..5).[1] { |n| n [2] n } puts squares.inspect
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using select which filters elements instead of transforming.
Using + which adds numbers instead of multiplying.
✗ Incorrect
The map method transforms each number by multiplying it by itself.
5fill in blank
hardFill all three blanks to select even numbers and double them.
Ruby
result = (1..10).[1] { |x| x [2] 2 == 0 }.[3] { |x| x * 2 } puts result.inspect
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using filter which is not a Ruby array method.
Using + instead of % to check evenness.
✗ Incorrect
First, select filters even numbers using the modulo operator %, then map doubles each selected number.