0
0
Rubyprogramming~10 mins

Array sorting and reversing 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 sort the array in ascending order.

Ruby
numbers = [5, 3, 8, 1]
sorted_numbers = numbers.[1]
Drag options to blanks, or click blank then click option'
Ashuffle
Bsort
Creverse
Dpop
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'reverse' instead of 'sort' will reverse the array but not sort it.
2fill in blank
medium

Complete the code to reverse the order of elements in the array.

Ruby
letters = ['a', 'b', 'c', 'd']
reversed_letters = letters.[1]
Drag options to blanks, or click blank then click option'
Areverse
Bpop
Csort
Dshift
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'sort' will reorder elements alphabetically, not reverse them.
3fill in blank
hard

Fix the error in the code to sort the array in descending order.

Ruby
scores = [10, 40, 30, 20]
desc_scores = scores.sort.[1]
Drag options to blanks, or click blank then click option'
Apop
Bsort
Cshuffle
Dreverse
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to use 'sort' twice or using 'shuffle' which randomizes the array.
4fill in blank
hard

Fill both blanks to create a new array with sorted elements and then reversed.

Ruby
data = [7, 2, 9, 4]
result = data.[1].[2]
Drag options to blanks, or click blank then click option'
Asort
Breverse
Cpop
Dshift
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'pop' or 'shift' which remove elements instead of sorting or reversing.
5fill in blank
hard

Fill all three blanks to sort the array, reverse it, and then get the first element.

Ruby
values = [15, 3, 9, 7]
first = values.[1].[2].[3]
Drag options to blanks, or click blank then click option'
Asort
Breverse
Cfirst
Dlast
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'last' instead of 'first' will get the smallest element after reversing.