# Negative index in array and Infinity

`````` /*jshint esversion: 6 */
function pairwise(arr, arg) {
var min = 0;
var ind = [];
var minval = 0;
var found = false;
var ret = 0;
for(i=0;i<arr.length;i++){
found = false;
if(arr[i] && arr[i]!==undefined){
var poss =[];
for(j=0;j<arr.length;j++){
if(arr[j]!==undefined){
var sum = arr[i]+arr[j];
if(i!=j && sum==arg){
found = true;
var indsum = i+j;
ind.push(indsum);
}
}
}
if(ind!=[]){
ret = Math.min(...ind);
}
console.log("Ret is:",ret);
min = ret - i;
if(found==true){
arr[min] = undefined;
arr[i] = undefined;
console.log(arr);
}
}
}
const reducer = (accumulator, currentValue) => accumulator + currentValue;
if(ind!==[]){
minval = ind.reduce(reducer,0);
}
return minval;
}
``````

Iâ€™ve got two issues. First is that I sometimes get Infinity as ret and the second is that sometimes arr has a â€ś-1â€ť index. I donâ€™t understand do they happenâ€¦ Any help would be appreciated.

Could you tell us which input gives you Infinity and which gives you the -1 index?

For the â€ś-1â€ť the 4th and for the Infinity the 1st.

Iâ€™m still trying to digest exactly what you are doing here, but this line caught my attention:

``````if(ind!==[])
``````

You canâ€™t compare arrays this way. Objects/arrays are references so you cannot check for equality like you do for primitives. What you are asking here is if the memory address of ind is the same as the memory address of an empty array you just created. You want:

``````if (ind.length)
``````

That will return true as long as the length is not equal to 0.

As `i` grows, it can be greater than `ret`, which is how youâ€™re getting negative numbers.

``````min = ret - i;
``````

Iâ€™m not able to reproduce the Infinity bug

With this line Iâ€™m trying to â€śget backâ€ť the j where I found the match/minimum in the second loop.

This is my poor attempt at doing something for every other element except arr[i].

Hi,

Iâ€™m not seeing the infinity bug in your program but I will make a suggestion.

Iâ€™ve seen a lot of solutions to PairWise (some of them mine) that rely on nested for loops and lots of temporary storage to hold indices, etc. Even when I know that they work it is often hard to wrap my mind around how they work.

I would suggest you consider using some of the built in Array methods. The one suggested in the challenge is .reduce(). That one and maybe a few others would go a long way to eliminating the nested for loops and if statements.

Why?

• Most of these methods loop through an array on their own without any need for you to write a for loop. Most keep up with the current index and element you are currently operating on for you - no i++, no arr[ i ][ j ]. Readability goes way up so code is easier to understand.

• These methods have descriptive names so they are practically self-commenting. Names like .find, .indexOf or .forEach go a long way to clarifying what each part of your function is actually doing.

• These do one thing well in a predictable manner. Even if they canâ€™t satisfy your request then they â€śfailâ€ť in a predictable manner too. For example, if .indexOf finds your target it returns an index number, if it cannot, then it returns -1. Simple and easy to test for.

Time spent learning about Array methods (and Object methods and String methods) and callbacks is time well spent.

Hereâ€™s the Array method suggested in the challenge - reduce â€śto reduce it to a single value.â€ť Thatâ€™s what you want in the end - a single value.

These below you may find of use depending on your logic

The full list of Array methods is on the left of each of these pages. Hover over the links to see a short description of how each one works.

Thatâ€™s my opinion on the subject. Iâ€™m no guru so Iâ€™m sharing, not judging. In the end you have to do whatever works for you. Hope this helps you some.
Good luck!

1 Like

I started over and refactored as you suggested and I got it! Thanks for your help! (I think Iâ€™m starting to wrap my head around how reduce works now!).

1 Like