August 13th, 2024
What's missing in this code?
// moveZeroesToEnd([0, 0, 1, 0, 3]) -> [ 1, 3, 0, 0, 0 ]
// moveZeroesToEnd([1, 0, 8, 0, 5, 0]) -> [ 1, 8, 5, 0, 0, 0 ]
// moveZeroesToEnd([3, 7, 4]) -> [ 3, 7, 4 ]
function moveZeroesToEnd(arr)
let count = 0; // Count of non-zero elements
// Shift non-zero elements to the beginning of the array
for (let i = 0; i < arr.length; i++)
if (arr[i]
0)
arr[
] = arr[
];
}
}
// Fill the remaining positions with zeros
while (count
arr.length)
arr[count++] = 0;
}
return arr;
Type or select from these options