I have an array that has zeroes in random indexes. The indexes for given different arrays are unknown. I used the following for loop to find and remove the zeros, so the resulting array is the same one without the zeros.
for (let i=0; i< arr.length; i++){
if(arr[i] === 0){
itemIndex = arr.indexOf(arr[i]);
arr.splice(itemIndex, 1); //removes the zero at the index
i = 0; //resets the i to zero so that every item in the array is searched even after it is altered.
}
}
As you can see, it resets the "i" to zero so that I can iterate through the array again because it will be altered and indexes of the zeroes will change. I am wondering if there is a better way to do this? I have a feeling that this could be coded better.