La matriz dada que se ejecutará aquí es [0, 1, 0, 3, 12] .
var moveZeroes = function(nums) { for (let i=0; i < nums.length; i++){ if (nums[i] === 0){ nums.push(0); console.log(nums) console.log(i) nums.splice(i, 1) } } }; moveZeroes([0, 1, 0, 3, 12]);El resultado es
[0, 1, 0, 3, 12, 0] ›0 ›[1, 0, 3, 12, 0, 0] ›1 ›[1, 3, 12, 0, 0, 0] ›3 ›[1, 3, 12, 0, 0, 0] ›4 ›[1, 3, 12, 0, 0]No entiendo cómo funciona el código.
Supongo que desea mover todos los ceros al final de la matriz. La i indica el número actual del índice en el ciclo. En cada ciclo, i aumentará en 1, y dado que movió todos los ceros al final de la matriz, la condición if se ejecutará y le mostrará el número 4 al final del ciclo for . Esta pregunta muestra que necesita conocer los fundamentos de la matriz y el bucle for. estos enlaces a continuación lo ayudarán a comprender cómo funcionan los bucles for y las matrices
Veamos tu código
var moveZeroes = function(nums) { for (let i = 0; i < nums.length; i++) { if (nums[i] === 0) { // push new 0 to the end of given array nums.push(0); // print the changed array console.log(nums) //print the current iteration number console.log(i) // in this case the splice method will remove the // element in index `i`. the argument `1` indicates // to remove 1 element after the index `1` nums.splice(i, 1) } } }; moveZeroes([0, 1, 0, 3, 12]);El siguiente código hará lo mismo sin mutar la matriz original
function moveZeroes(nums) { const newArray = [] // the loop will iterate based on the length of the array plus // the number of zeros for (let i = 0, j = 0; i < nums.length + j; i++) { // push the non-zero numbers to the new array if (i < nums.length && nums[i]) newArray.push(nums[i]) // count the zeros else if (i < nums.length) j++; // push zeros to the end of the array when the iteration // is beyond the length of the array else newArray.push(0); } return newArray }; console.log(moveZeroes([0, 1, 0, 3, 12]))Según la pregunta, solo tiene dos ceros, pero estoy mostrando 4 porque, está iterando a través de la matriz por bucle, e indico el índice aquí con el que identificamos la posición de los elementos de la matriz. El índice de la matriz comienza desde 0 y la matriz tiene un total de 5 elementos, por lo tanto. en la última iteración seré 4
const moveZeroes = function(nums) { // Iterate through each element of the array and save the index on `i` for (let i = 0; i < nums.length; i++) { // If the current element of the iteration has value 0, execute... if (nums[i] === 0) { // Push a `0` at the end of the array nums.push(0); // Print current index of the loop console.log(i) // Print current state of the array. At this point the array would // have length = 6 since we have added a `0` at the end of the array console.log(nums) // In this case the splice function is used to remove a element // of the array at index `i`. The argument `1` means to remove 1 element // from index `i` nums.splice(i, 1) } } }; const array = [0, 1, 0, 3, 12]; // Take into account that I'm passing the array by reference // (not by value). It means that the function would modify the `array` elements moveZeroes(array); console.log("Result", array);