Inventory Update Question

Inventory Update Question
0.0 0

#1

I’m so close to my final answer, I just have one last problem. My first if statement only adds the inventory to the first time it recognizes the element in arr2 is already in arr1. This is my only issue left. Here is my code:


function updateInventory(arr1, arr2) {
      var x = 0;
  while (x<arr2.length) { // two loops are so it can loop through every element in arr2 through the first element 
    for(var i = 0; i<arr1.length; i++) { //of arr 1, and then so on. 
      if(arr1[i].indexOf(arr2[x][1]) > 0) {
        arr1[i][0] += arr2[x][0];
        x++;
      } else if (arr1[i].indexOf(arr2[x][1]) < 0) {
        arr1.push(arr2[x]);
        break;
      }
    }  
     x++; //loops here so that all possible values can be considered.
  } 
    return arr1;
}

// Example inventory lists
var curInv = [
    [21, "Bowling Ball"],
    [2, "Dirty Sock"],
    [1, "Hair Pin"],
    [5, "Microphone"]
];

var newInv = [
    [2, "Hair Pin"],
    [3, "Half-Eaten Apple"],
    [67, "Bowling Ball"],
    [7, "Toothpaste"]
];

updateInventory(curInv, newInv);