Search⌘ K
AI Features

Exercise 6: Reverse the Array

Explore how to reverse the elements of an array in Ruby by applying built-in array methods. Learn to transform an array so its elements appear in the opposite order, enhancing your understanding of Ruby's array manipulation capabilities.

We'll cover the following...

Problem statement

Reverse the array so that the first element appears last, the second element appears second to last, and so on.

Example

input_array = [1, 2, 3, 4, 5]
result =  [5, 4, 3, 2, 1]

Note: Look through the methods listed in the Array documentation to see if any of these describes what we’re looking for.

Try it yourself

Ruby
def reverse_elements(input_array)
result = []
# Start your code here
return result
end