Practice - 5 Tasks
Answer the questions below
1fill in blank
easyComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'reverse' instead of 'sort' will reverse the array but not sort it.
✗ Incorrect
The sort method arranges the elements of the array in ascending order.
2fill in blank
mediumComplete 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'sort' will reorder elements alphabetically, not reverse them.
✗ Incorrect
The reverse method flips the order of elements in the array.
3fill in blank
hardFix 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Trying to use 'sort' twice or using 'shuffle' which randomizes the array.
✗ Incorrect
To sort in descending order, first sort ascending, then reverse the array.
4fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'pop' or 'shift' which remove elements instead of sorting or reversing.
✗ Incorrect
First, sort arranges the array ascending, then reverse flips it to descending.
5fill in blank
hardFill 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'
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'last' instead of 'first' will get the smallest element after reversing.
✗ Incorrect
We sort ascending, reverse to descending, then get the first element which is the largest.