Duplicate Zeros LeetCode solution in JavaScript
1/**2* @param {number[]} arr3* @return {void} Do not return anything, modify arr in-place instead.4*/5var duplicateZeros = function (arr) {6// Variables7const originalLength = arr.length;89// Iterate over all the numbers in 'arr', if the number is 0 we duplicate it and skip the next loop iteration to prevent an overflow.10for (let idx = 0; idx < arr.length; idx++) {11const number = arr[idx];12if (number === 0) {13arr.splice(idx, 0, 0);14idx += 1;15}16}1718// Here we restore the array to its original length.19return (arr.length = originalLength);20};