Last active
October 29, 2015 00:27
-
-
Save greemwahr/d9f42a32a04c1c075a75 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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