Javascript Array move zeros to end

Introduction

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]
'use strict'//  w  w w.  j  ava2  s . c  o m

/*
var moveZeros = function(arr) {
  let zeroCounter = 0;

  arr = arr.filter(function(currentValue) {
    if(currentValue === 0) {
      zeroCounter++;
      return false;
    } else {
      return true;
    };
  });

  for(let i = 0, j = zeroCounter; i < j; i++) {
    arr.push(0);
  }

  return arr;
}
*/

var moveZeros = function(arr) {
  let nonZeros = arr.filter(function(currentValue) {return currentValue !== 0});
  let allZeros = arr.filter(function(currentValue) {return currentValue === 0});

  return nonZeros.concat(allZeros);
}



console.log(moveZeros([false, 1, 0, 1, 2, 0, 1, 3, 'a']));



PreviousNext

Related