Skip to content

Instantly share code, notes, and snippets.

@jlmferreira
Last active April 13, 2018 10:01
Show Gist options
  • Save jlmferreira/a9f03f83cd0b5beab92cd52f971ad065 to your computer and use it in GitHub Desktop.
Save jlmferreira/a9f03f83cd0b5beab92cd52f971ad065 to your computer and use it in GitHub Desktop.
Task - Switch zero's to the end of array
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