Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete the code to add the number 5 to the end of the array.
Ruby
numbers = [1, 2, 3, 4] numbers.[1](5) puts numbers.inspect
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop removes the last element instead of adding.
Using shift removes the first element.
Using unshift adds to the beginning, not the end.
✗ Incorrect
The push method adds an element to the end of the array.
2fill in blank
mediumComplete the code to remove the first element from the array.
Ruby
fruits = ['apple', 'banana', 'cherry'] removed = fruits.[1]() puts removed puts fruits.inspect
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop removes the last element, not the first.
Using push or unshift add elements instead of removing.
✗ Incorrect
The shift method removes and returns the first element of the array.
3fill in blank
hardFix the error in the code to add 'orange' to the beginning of the array.
Ruby
colors = ['red', 'green', 'blue'] colors.[1]('orange') puts colors.inspect
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using push adds to the end, not the beginning.
Using pop or shift remove elements instead of adding.
✗ Incorrect
The unshift method adds an element to the beginning of the array.
4fill in blank
hardFill both blanks to remove the last element and add 'kiwi' to the beginning of the array.
Ruby
basket = ['apple', 'banana', 'cherry'] basket.[1]() basket.[2]('kiwi') puts basket.inspect
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Using shift instead of pop removes the first element.
Using push instead of unshift adds to the end.
✗ Incorrect
pop removes the last element, and unshift adds an element to the beginning.
5fill in blank
hardFill all three blanks to add 10 to the end, remove the first element, and add 5 to the beginning of the array.
Ruby
nums = [1, 2, 3] nums.[1](10) removed = nums.[2]() nums.[3](5) puts nums.inspect puts removed
Drag options to blanks, or click blank then click option'
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up pop and shift for removing elements.
Using push instead of unshift to add at the beginning.
✗ Incorrect
push adds to the end, shift removes the first element, and unshift adds to the beginning.