Negative index in array and Infinity

Negative index in array and Infinity
0.0 0

#1
 /*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.


#2

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


#3

For the “-1” the 4th and for the Infinity the 1st.


#4

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.


#5

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 :frowning:


#6

With this line I’m trying to “get back” the j where I found the match/minimum in the second loop.


#7

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


#8

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!


#9

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!).