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 indexesToAdd = [ ];
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(i) === -1 &&
indexesToAdd.indexOf(j) === -1 ; j++) {
if (arr[i] + arr[j] === arg) {
if (indexesToAdd.indexOf(i) === -1) {
indexesToAdd.push(i);
}
if (indexesToAdd.indexOf(j) === -1) {
indexesToAdd.push(j);
}
}
}
}
for (var y = 0; y < indexesToAdd.length; y++) {
total += indexesToAdd[y];
}
return total;
}
pairwise([1, 4, 2, 3, 0, 5], 7);