Inventory Update help, issue with potential infinite loop

Inventory Update help, issue with potential infinite loop
0

#1

Hi can’t seem to figure out why the below else statement would cause an infinite loop, my logic was that if the item in arr2 is not equal to any of the items in arr1, the item itself would be pushed onto arr1 in the else statement, I have tried to put a variable counter3 in there consolelog how many times it matches the else statement, it matches 14x for some reason, where I was expect 2 as arr1 is missing 2 items from arr2. Thank you for your help


function updateInventory(arr1, arr2) {
    // All inventory must be accounted for or you're fired!
  var counter1;
  var counter2;
  var counter3=0;
  
  
  for ( counter1=0; counter1<arr1.length; counter1++){
    
    for (counter2=0; counter2<arr2.length; counter2++) {
      
      if ( arr2[counter2][1] == arr1[counter1][1]) {
                                             
                                   
        
        
         arr1[counter1][0]=arr1[counter1][0]+arr2[counter2][0];
        //counter3++;
         //console.log(counter3);
        
        
      }
      
      else {
                
                
                counter3++;
        console.log(counter3);
        arr1.push(arr2[counter2]); //this part causing the infinite loop warning
               
      
      }
                                   
      
      
      
      
      
      
    }
    ```

#2

Pushing values to arr1 increases its length, also increasing the number of times the outer for-loop has to run, which also means the inner for-loop will have to make another round of looping.


#3

Took out that line so the arr1 length no longer increases but found that the counter which I have placed there still gets executed 14x.

Also tried to push the non matching result into a temp_arr to test and found that it’s got 14 results with matching and non matching items. Unsure if this is a glitch or some logic error in my double for loops.