# Pairwise driving me crazy! Please help!

**URL:** <https://forum.freecodecamp.org/t/pairwise-driving-me-crazy-please-help/49192>\
**Category:** General\
**Created:** [October 26, 2016, 11:46pm UTC](https://forum.freecodecamp.org/t/pairwise-driving-me-crazy-please-help/49192 "2016-10-26T23:46:05Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![SolaScriptura92](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/solascriptura92/32/120561_2.png) [@SolaScriptura92](https://forum.freecodecamp.org/u/SolaScriptura92)\
**Post date:** [October 26, 2016, 11:46pm UTC](https://forum.freecodecamp.org/t/pairwise-driving-me-crazy-please-help/49192/1 "2016-10-26T23:46:05Z")

</div>

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.

```auto
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);

```

---

<div class="post-metadata">

**Author:** ![xavierartot](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/xavierartot/32/160720_2.png) [@xavierartot](https://forum.freecodecamp.org/u/xavierartot)\
**Post date:** [October 27, 2016, 2:06am UTC](https://forum.freecodecamp.org/t/pairwise-driving-me-crazy-please-help/49192/2 "2016-10-27T02:06:17Z")

</div>

can you indent your code?

---

<div class="post-metadata">

**Author:** ![SolaScriptura92](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/solascriptura92/32/120561_2.png) [@SolaScriptura92](https://forum.freecodecamp.org/u/SolaScriptura92)\
**Post date:** [October 27, 2016, 5:41am UTC](https://forum.freecodecamp.org/t/pairwise-driving-me-crazy-please-help/49192/3 "2016-10-27T05:41:18Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jenovs](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jenovs/32/60963_2.png) [@jenovs](https://forum.freecodecamp.org/u/jenovs)\
**Post date:** [October 27, 2016, 7:07am UTC](https://forum.freecodecamp.org/t/pairwise-driving-me-crazy-please-help/49192/4 "2016-10-27T07:07:30Z")

</div>

This line:

```auto
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]

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

  if (!arr.length) return 0;

  var indexesToAdd = [];
  //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 (indexesToAdd.indexOf(i) === -1 && indexesToAdd.indexOf(j) === -1) {
        if (arr[i] + arr[j] === arg && i !== j) {
          indexesToAdd.push(i);
          indexesToAdd.push(j);
        }
      }
    }
  }
  return indexesToAdd.reduce((a, b) => a + b);
}

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

```

[/Details]

---

<div class="post-metadata">

**Author:** ![SolaScriptura92](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/solascriptura92/32/120561_2.png) [@SolaScriptura92](https://forum.freecodecamp.org/u/SolaScriptura92)\
**Post date:** [October 27, 2016, 2:03pm UTC](https://forum.freecodecamp.org/t/pairwise-driving-me-crazy-please-help/49192/5 "2016-10-27T14:03:51Z")

</div>

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! 😃

---

<div class="post-metadata">

**Author:** ![jwilkins.oboe](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jwilkins.oboe/32/179497_2.png) [@jwilkins.oboe](https://forum.freecodecamp.org/u/jwilkins.oboe)\
**Post date:** [January 16, 2021, 5:31am UTC](https://forum.freecodecamp.org/t/pairwise-driving-me-crazy-please-help/49192/6 "2021-01-16T05:31:01Z")

</div>

Closed on January 16, 2021, 5:31am UTC


