Move Zeroes

Challenge from issue #157 of rendezvous with cassidoo.

by Jesse Rogers

JavaScript

/**
 * issue #157 of rendezvous with cassidoo
 */

const source = [1, 2, 0, 1, 0, 0, 3, 6];

function moveZeros(input) {
	// loop backwards because we are moving/skipping indexes
  for (let i = input.length - 1; i >= 0; i--) {
  	// is it zero or a hero
    if (input[i] === 0) {
    	// splice out the zero and push to end
    	input.push(input.splice(i, 1)[0]);
    }
  }
  // send back the altered input
  return input;
}

// yuh
console.log(moveZeros(source));