0
0
Rubyprogramming~10 mins

Array modification (push, pop, shift, unshift) 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 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'
Aunshift
Bpop
Cpush
Dshift
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.
2fill in blank
medium

Complete 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'
Apop
Bunshift
Cpush
Dshift
Attempts:
3 left
💡 Hint
Common Mistakes
Using pop removes the last element, not the first.
Using push or unshift add elements instead of removing.
3fill in blank
hard

Fix 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'
Aunshift
Bpop
Cpush
Dshift
Attempts:
3 left
💡 Hint
Common Mistakes
Using push adds to the end, not the beginning.
Using pop or shift remove elements instead of adding.
4fill in blank
hard

Fill 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'
Apop
Bpush
Cunshift
Dshift
Attempts:
3 left
💡 Hint
Common Mistakes
Using shift instead of pop removes the first element.
Using push instead of unshift adds to the end.
5fill in blank
hard

Fill 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'
Apop
Bshift
Cunshift
Dpush
Attempts:
3 left
💡 Hint
Common Mistakes
Mixing up pop and shift for removing elements.
Using push instead of unshift to add at the beginning.