Skip to content

Latest commit

 

History

History
26 lines (19 loc) · 728 Bytes

moving-zeros-to-the-end.md

File metadata and controls

26 lines (19 loc) · 728 Bytes

Moving Zeros To The End 5 Kyu

LINK TO THE KATA - ARRAYS SORTING ALGORITHMS

Description

Write an algorithm that takes an array and moves all of the zeros to the end, preserving the order of the other elements.

moveZeros([false, 1, 0, 1, 2, 0, 1, 3, 'a']) // returns[(false, 1, 1, 2, 1, 3, 'a', 0, 0)]

Solution

const moveZeros = arr => {
  const arrayWithoutZeros = arr.filter(item => item !== 0)
  const arrayOfZeros = arr.filter(item => item === 0)

  return [...arrayWithoutZeros, ...arrayOfZeros]
}