Pairwise help please

the problem here is that the second loop loops through the array and finds any matches for arr[i] and cuz neededNum is already defined the arr[i] change line isn’t recognized in the computer because neededNum won’t be changed until the for j loop is finished, i think.
how do i fix this ?(i got what’s the problem after some time from the post so that’s an edit)

Your code so far

function pairwise(arr, arg) {
var res = 0;
for (let i = 0; i < arr.length; i++) {
var neededNum = arg - arr[i];
for (let j = i + 1; j < arr.length; j++) {
if (arr[j] === neededNum) {
res += arr.indexOf(arr[i]) + arr.indexOf(arr[j]);
arr[j] = arg+1; //putting the matches in a "black list"
arr[i] = arg+1;
return res

console.log(pairwise([1, 1, 1], 2)); //logs 2
console.log(pairwise([0, 0, 0, 0, 1, 1], 1)); //logs 9

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/86.0.4240.75 Safari/537.36.

Challenge: Pairwise

Link to the challenge:

These two lines:

change the array and what is the importance of these two lines؟

to make sure the same elements (particularly the j one) don’t get paired more than one time, there’s something wrong with them? if i change it to an empty string it doesnt really change anything

The problem is the first loop is loop over the same index of j and it may be use the same value twice.

@harel_avv Didn’t you solve it yet.

The three tests this passes are the ones where there are just one way to pair the numbers and it picks them correctly. The two tests you’re having problems with are the two where there are multiple ways to pick the pairs and you have to ensure the lower index pair is found first.

This blacklisting idea is a good one, and this implementation is easier than one I did using null.

There are two problems. One, when you find a match on the inner loop, you need to stop looking and change your first number to the next available number before continuing the search. If you keep looking, you may find other pairs with higher indices.

Two, when you call indexOf()

you already know the answer (i and j), but worse, indexOf() returns the index of the first matching value. If arr[i] and arr[j] have the same value, indexOf() will return the same index for both and you need two different indices.

Good luck.

for the index one, i’m such a dummy :joy:, didn’t get enough sleep lol, and as for the first problem, i defined neededNum in the start of the inner loop so that it will be updated, thanks a lot!