Solution
function pairwise(arr, arg) {
let sumIndices = 0; // running sum of indices matched
let matchedPairs = []; // stores indices of matched pairs
arr.forEach((elem, index) => {
// check sum with array elements that follow elem
for (let i = index+1; i < arr.length; i++) {
if ((elem + arr[i]) == arg) {
// check that array index hasn't been used already
if (!matchedPairs.some(mp => (mp == index || mp == i))) {
sumIndices += (index + i);
matchedPairs.push(index, i)
}
}
}
})
return sumIndices;
} // function pairwise
// test here
pairwise([1,4,2,3,0,5], 7); // returns 11
Challenge: Pairwise
Link to the challenge: