Skip to content
Exercise · Arrays · lesson 10 of 13

Exercise: reversed array

Write a function function reversed(nums) that returns a new array with the elements of nums in reverse order. The function does not change the array nums itself.

  • reversed([1, 2, 3]) → [3, 2, 1]
  • reversed([5]) → [5]
  • reversed([]) → []

Constraints: nums holds from 0 to 1000 integers, which may repeat and may be negative; for an empty array the result is an empty array; after the call, nums holds the same elements in the same order.

The tests print results through JSON.stringify: it turns an array into a string without spaces, for example [3, 2, 1] into [3,2,1], and puts the string values inside the array in quotes.

Do not add extra imports unless the task says so explicitly.

reversed.js — javascript
javascript · 4 lines
Ctrl+Enter run · Esc leave editor

Subscribe to take the course

The course is free. Sign in with Telegram and subscribe to the channel to open every lesson.

Sign in another way