Skip to content

Instantly share code, notes, and snippets.

@greemwahr
Last active October 29, 2015 00:27
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save greemwahr/d9f42a32a04c1c075a75 to your computer and use it in GitHub Desktop.
Save greemwahr/d9f42a32a04c1c075a75 to your computer and use it in GitHub Desktop.
function move_zeros(arrNum, isRight){
isRight = typeof isRight === 'undefined' ? true : isRight;
// console.log(arrNum);
// console.log(isRight);
var arrNumLength = arrNum.length;
function eliminateZeros(element) {
return element !== 0;
}
var arrayWithoutZeros = arrNum.filter(eliminateZeros);
var arrNumLengthWithoutZeros = arrayWithoutZeros.length;
var numberOfZerosToAdd = arrNumLength - arrNumLengthWithoutZeros;
// console.log(arrayWithoutZeros);
// console.log(numberOfZerosToAdd);
var i = 1;
var shiftZeros = arrayWithoutZeros;
// console.log(shiftZeros);
var tmpSwitchCaseArray = arrayWithoutZeros;
switch(isRight) {
case true:
while (i <= numberOfZerosToAdd) {
tmpSwitchCaseArray.push(0);
i++;
}
break;
case false:
while (i <= numberOfZerosToAdd) {
tmpSwitchCaseArray.unshift(0);
i++;
}
break;
}
return tmpSwitchCaseArray;
}
arrNum = [12, 0, 10, 0, 8, 12, 7, 6, 0, 4, 10, 12, 0];
move_zeros(arrNum);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment