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.
numbersis 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 ]
everySecondFromEnd([1, 2, 3, 4])Returns [ 4, 2 ]
Asked at Capgemini · Source
Loading the editor...
- Input
everySecondFromEnd([ 1, 10, 24, 17, 82, 40, 12, 8412, 81212, 93, 9, 13, 46 ])Expected[ 46, 9, 81212, 12, 82, 24, 1 ] - Input
everySecondFromEnd([ 1, 2, 3, 4 ])Expected[ 4, 2 ] - Input
everySecondFromEnd([])Expected[] - The input array is not changed, the result is a new array
Hidden tests run on Submit