Last active
April 13, 2018 10:01
-
-
Save jlmferreira/a9f03f83cd0b5beab92cd52f971ad065 to your computer and use it in GitHub Desktop.
Task - Switch zero's to the end of array
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
list = [2,4,0,11,0,1,2,0,30,0]; | |
count = 0; | |
tail = list.length - 1; | |
firstZero = -1; | |
while(count <= tail){ | |
if(list[count] == 0 && firstZero == -1){ | |
firstZero = count; | |
}else if(list[count] != 0 && firstZero != -1 ){ | |
var b = list[count]; | |
list[count] = list[firstZero] ; | |
list[firstZero] = b ; | |
firstZero +=1; | |
} | |
count +=1; | |
} | |
/* | |
or | |
list.sort(function(a,b){return b-a}); | |
*/ | |
console.log(list); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment