moveZeros

codewars 5kyu

by trentHarlem

HTML

<h1>
Moving Zeros
</h1>
<p>
Write an algorithm that takes an array and moves all of the zeros to the end, preserving the order of the other elements.
</p>

JavaScript

const moveZeros = (arr) => arr.filter(item => item !== 0).concat(arr.filter(item => item === 0))

console.log(moveZeros([false, 1, 0, 1, 2, 0, 1, 3, "a"]), 'returns[false,1,1,2,1,3,a,0,0]')
  
 // my first 6kyu without having to search or leave the page

/*
const moveZeros = function (arr) {
   return arr.map((item, i) => {
          if (item===0) {
          arr.push(item)
          }
          })
          return arr
} */