Given array to be executed here is [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]);
Result is
[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]
I am not understanding how is the code working.
I assume you want to move all the zeros to the end of the array. The i indicates the current number of the index in the loop. In each loop, the i will increase by 1, and since you moved all the zeros to the end of the array, the if condition will run and show you the number 4 at the end of the for-loop. This question shows that you need to know the fundamentals of the array and for-loop. these links below will help you to grasp how for-loops and arrays work
Let's look at you code
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]);
The code below will do the same thing without mutating the original array
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]))
As per the question you have only two zeros but i is showing 4 because, your iterating through array by loop , and i indicates index here with which we identify the position of array elements.array index start from 0 and array has total 5 elements hence at the last iteration i will be 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);