Created
May 17, 2019 18:31
-
-
Save jasonwaters/442b0016df0b3112d020dd2384ddc5d3 to your computer and use it in GitHub Desktop.
Move non-zeroes to the left of an 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
const a = [2,3,0,0,0,0,0,2,3,4,0,2,0,3,0,1,8]; | |
function leftify(arr) { | |
let leftmost = 0; | |
arr.forEach((el, idx) => { | |
if(el !== 0) { | |
const elements = arr.splice(idx,1); | |
arr.splice(leftmost,0,...elements); | |
leftmost++; | |
} | |
}) | |
return arr; | |
} | |
console.log(leftify(a)); |
Author
jasonwaters
commented
May 22, 2019
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment