A few things.
- If you only want to perform an action once, there is no need for a loop. Loops are for when you want to perform the same logic over and over, for each item in a collection. Notice how you don’t actually need to use
valuesin your logic. - You are mutating the array while you are looping over it. That can get you all sorts of wacky logical errors, including infinite loops.
- The
if (secondArr.indexOf(values) === n) {is a bit perplexing to me. I would guess that the intention was to only do thespliceonce, but from what you’ve shared it doesn’t seem like there’s any guarantee that every value will be unique, so you could perform that action multiple times. This is also a case where the fact that you are mutating the array as you go could cause problems.