Code Lab

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

Negative numbers to the left

MédiaArrays10 min

Write a function groupNegatives(numbers) that returns a new array with the negative numbers on the left: first all negative numbers, then all other numbers.

  • numbers is an array of integers. It can be empty.
  • 0 is not negative: it goes to the right group, together with the positive numbers.
  • Keep the order inside each group as in numbers. [-5, 10, -3, 8, -1, 6] gives [-5, -3, -1, 10, 8, 6]. Do not sort the numbers.
  • Return a new array. Do not change (do not mutate) numbers.

Each element goes to one of two groups. filter can pick the elements of one group.

Exemplos

groupNegatives([-5, 10, -3, 8, -1, 6])

Retorna [ -5, -3, -1, 10, 8, 6 ]

groupNegatives([4, -7, 0, 2])

Retorna [ -7, 4, 0, 2 ]

Cobrada em ServiceNow · Fonte

Carregando o editor...
  1. EntradagroupNegatives([ -5, 10, -3, 8, -1, 6 ])
    Esperado[ -5, -3, -1, 10, 8, 6 ]
  2. EntradagroupNegatives([ 4, -7, 2 ])
    Esperado[ -7, 4, 2 ]

Os testes ocultos rodam no envio