Code Lab

Modo entrevista. Cronômetro de 5 min, sem dicas e sem autocompletar

Every second number from the end

FácilBasics5 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 [].

Exemplos

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

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

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

Retorna [ 4, 2 ]

Cobrada em Capgemini · Fonte

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

Os testes ocultos rodam no envio