Clarification on the pairwise challenge

Clarification on the pairwise challenge


Hey guys, I was just wondering if anyone could provide some clarification for me on the solution to the Pairwise challenge. I figured it out, I think. It works. But I don’t understand the basic solution on the “Get a hint” page. It sums k and i, but those aren’t the indices, right? Those are just whatever iteration of the array. So I don’t understand where in the code it is the indices being added, and I’d just really like clarification because I don’t like not understanding things, even if I somehow did it successfully anyway. Thanks!

The solution I’m referring to:

function pairwise(arr, arg) {
 // Set sum of indices to zero
 var sum = 0;
 // make a local copy of the arguments object so we don't modify it directly
 var pairArr = arr.slice();
 // looping from first element
 for(i = 0; i < pairArr.length; i++) {
   //Looping from second element by setting first element  constant
   for(j = i + 1; j < pairArr.length; j++) {
     // Check whether the sum is equal to arg
     if(pairArr[i] + pairArr[j] == arg) {
       //Add the indices
       sum += i + j;
       //Set the indices to NaN so that they can't be used in next iteration
       pairArr[i] = pairArr[j] = NaN;
 return sum;

// test here
pairwise([1,4,2,3,0,5], 7);


You meant j, not k? Anyway, those are indeed the indices of the array. That’s why pairArr[i] and pairArr[j] give you the contents of the array.


When you iterate through an array. You’re iterating through elements of an array. Those elements are sitting on the indices of the array.

On a side note, pairwise has got to be the simplest advanced algorithm took me like 2 minutes to solve.