it happens if you change the array you are iterating over
like, let’s say we have array [1,2,4,5,6,8] and we need to remove even numbers that are
index 0 - there is 1, it’s not even, it stays here
index 1 - there is 2, we need to remove this. Now the array is [1,4,5,6,8]
index 2 - there is 5, it’s not even, it stays here
index 3 - there is 6, we need to remove this. Now the array is [1,4,5,8]
index 4 - there isn’t an index 4, array ended. Result is [1,4,5,8]
do you notice what happened? the 4 went to index 1, which was already checked, so it was skipped. Same for the 8
i suggest that you use the for loop becoz the for…of loop is kinda complicated the apply the push method on those elements which aren’t in the array, happy coding camper!
This is definitely a good time to use higher order elements. I was trying to use a combination of a for loop and splice like you when I realized it would be much easier to use the filter method because that’s what it is essentially doing with a lot less work. Think about it - it’s iterating over the items and eliminating them.