I have no idea why my function doesn't work in some cases:
function moveZeros(elem) {
var count = 0;
var a=elem;
for (var i=0; i< elem.length; i++){
if (elem[i]===0) {
elem.splice(i,1);
count++;
}
}
while (count>0) {
elem.push(0);
count--;
}
return elem;
}
In moveZeros([1,2,0,1,0,1,0,3,0,1]) all good, but if case is:
moveZeros([9,0.0,0,9,1,2,0,1,0,1,0.0,3,0,1,9,0,0,0,0,9])
it returns
[9,0,9,1,2,1,1,3,1,9,0,0,9,0,0,0,0,0,0,0]
I case :
moveZeros(["a",0,0,"b","c","d",0,1,0,1,0,3,0,1,9,0,0,0,0,9]) it returns: ["a",0,"b","c","d",1,1,3,1,9,0,0,9,0,0,0,0,0,0,0]
Why not all zeros goes to the end ?