Negative numbers to the left
MediumArrays10 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.
numbersis an array of integers. It can be empty.0is 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.
Examples
groupNegatives([-5, 10, -3, 8, -1, 6])Returns [ -5, -3, -1, 10, 8, 6 ]
groupNegatives([4, -7, 0, 2])Returns [ -7, 4, 0, 2 ]
Asked at ServiceNow · Source
Loading the editor...
- Input
groupNegatives([ -5, 10, -3, 8, -1, 6 ])Expected[ -5, -3, -1, 10, 8, 6 ] - Input
groupNegatives([ 4, -7, 2 ])Expected[ -7, 4, 2 ]
Hidden tests run on Submit