0

#1

The following is my code, can someone please tell me why it will not work for the following case at the bottom? It works well for the other test cases.

``````function pairwise(arr, arg) {

var total = 0;
//The first loop below should go through each number in the array.
for (var i = 0; i < arr.length; i++) {
//now the inner loop will check if the adding to each number in array will equal arg. If it does, and the indexes do not match numbers that are already in the array, push both indexes to indexestoadd array.
for (var j = 1; j < arr.length &&

indexesToAdd.indexOf(j) === -1 ; j++) {
if (arr[i] + arr[j] === arg) {
}
}
}
}

}

for (var y = 0; y < indexesToAdd.length; y++) {
}

}

pairwise([1, 4, 2, 3, 0, 5], 7);
``````

#2

#3

I tried to through my phone but I guess it didn’t work. I will do it on my computer tomorrow, Lord willing. Thanks for your response.

#4

This line:

``````for (var j = 1; j < arr.length && indexesToAdd.indexOf(i) === -1 &&  indexesToAdd.indexOf(j) === -1; j++)
``````

If you have something in your `indexesToAdd` array your loop terminates.

[Details=Spoiler]

``````function pairwise(arr, arg) {

if (!arr.length) return 0;

//The first loop below should go through each number in the array.
for (let i = 0; i < arr.length; i++) {
//now the inner loop will check if the adding to each number in array will equal arg.
//If it does, and the indexes do not match numbers that are already in the array, push both indexes to indexestoadd array.
for (let j = 0; j < arr.length; j++) {
if (arr[i] + arr[j] === arg && i !== j) {
}
}
}
}
return indexesToAdd.reduce((a, b) => a + b);
}

pairwise([1, 4, 2, 3, 0, 5], 7);
``````

[/Details]

#5

Ahhhh that’s what I thought the problem was but I was not sure how to go about solving it! Thank you so much for your help!