Code Lab

Every second number from the end

EasyBasics5 min

Write a function everySecondFromEnd(numbers) that goes through an array from the end and takes every second element.

  • numbers is an array of integers. It can be empty.
  • Return a new array: first the last element, then the third from the end, then the fifth from the end, and so on.
  • Do not change numbers: after the call it must have the same elements in the same order.
  • For an empty array return [].

Examples

everySecondFromEnd([1, 10, 24, 17, 82, 40, 12, 8412, 81212, 93, 9, 13, 46])

Returns [ 46, 9, 81212, 12, 82, 24, 1 ]

The list from the interview. Like Node, the trainer prints a long array of numbers in columns: the result is 46, 9, 81212, 12, 82, 24, 1.
everySecondFromEnd([1, 2, 3, 4])

Returns [ 4, 2 ]

Asked at Capgemini · Source

Loading the editor...
  1. InputeverySecondFromEnd([ 1, 10, 24, 17, 82, 40, 12, 8412, 81212, 93, 9, 13, 46 ])
    Expected[ 46, 9, 81212, 12, 82, 24, 1 ]
  2. InputeverySecondFromEnd([ 1, 2, 3, 4 ])
    Expected[ 4, 2 ]
  3. InputeverySecondFromEnd([])
    Expected[]
  4. The input array is not changed, the result is a new array

Hidden tests run on Submit.