+ 2

Why are duplicate matches not removed properly?

I have a forEach method checking match threes to see if they’re part of a match four and popping them out of the array if they are. Duplicate matches are detected correctly but the wrong one is removed. Why? https://code.sololearn.com/WIGYx83rvJhh/?ref=app

20th Jan 2022, 5:07 PM
Margaret Guzman
Margaret Guzman - avatar
2 Respuestas
+ 2
You are using pop(item), to remove the matching array, but this is incorrect because pop() only removes the last item or the array and it does not take any parameter. https://www.w3schools.com/jsref/jsref_pop.asp I suggest using splice() and the index of the item to remove correct match https://www.w3schools.com/jsref/jsref_splice.asp [edited] But keep in mind that matchThrees array will be mutated every-time there is a match, this may cause the element of matchThrees to shift indexes right to left. You can use findIndex() to find the index of the item of the mutated matchThrees array https://code.sololearn.com/WUhagT3C1vL5
20th Jan 2022, 5:54 PM
ODLNT
ODLNT - avatar
0
Margaret Guzman I had to edit my initial reply, due to the fact that I did not take into account that the matchThrees array will be changed after a match is found. Sorry for any confusion this may have cause.
20th Jan 2022, 7:19 PM
ODLNT
ODLNT - avatar