Help needed in challenge: Inventory Update

Help needed in challenge: Inventory Update
0

#1

Hello,
I’m trying to solve the challenge Inventory Update.

This is the code I have written so far:

function updateInventory(arr1, arr2) {
    for(var i = 0; i < arr1.length; i++){
      for(var j = 0; j < arr2.length; j++){
        if(arr1[i][1] === arr2[j][1]){
          arr1[i][0] += arr2[j][0]; 
        }
        /*else if(arr1[i].indexOf(arr2[j][1]) === -1){
           arr1.push(arr2[j];
         }
          Something like this....
       */
      }
    }
    
    return arr1;
}

I’m stuck with the part, where I should add the elements in arr2 to arr1 (which are not in arr1). I think that this should be done with the indexOf function, but I cannot understand how should I solve it.

Any tips or suggestions would really be appreciated!


#2

You might try making an array consisting of all of items of arr1 (not the quantities) and then check whether arr2[j][1] is included in that array.

if (itemsFromArr1.includes(arr2[j][1])) {

//then just update the quantity
// here's where you'd use indexOf

} else {

// add the item with its quantity from arr2

}

I’m not sure that the nested for loops would be required with that.


#3

I’m not sure if this is what your meant. But I came up with this:

function updateInventory(arr1, arr2) {
  var itemsFromArr1 = [];
    for(var i = 0; i < arr1.length; i++){
      itemsFromArr1.push(arr1[i][1]);
      
      for(var j = 0; j < arr2.length; j++){
        if (itemsFromArr1.includes(arr2[j][1])) {
          arr1[i][0] += arr2[j][0];
         //then just update the quantity
         // here's where you'd use indexOf
        
        } else {
        //  arr1.push(arr2[j]);
        // add the item with its quantity from arr2
        }
      }
      
    }
    
    return arr1;
}


#4

I was thinking something like this:

function updateInventory(arr1, arr2) {
  var itemsFromArr1 = [];
    for(var i = 0; i < arr1.length; i++){
      itemsFromArr1.push(arr1[i][1]);
    }
    // stop the first for loop here. You want to make sure *all*
    // of the items from arr1 are in that itemsFromArr1 
    // before you start calling `includes` on it.
    
    
    //now, iterate through arr2 and check if each item
    // is already in itemsFromArr1
      for(var j = 0; j < arr2.length; j++){
        if (itemsFromArr1.includes(arr2[j][1])) {
          // arr1[i][0] += arr2[j][0];
          //now you need to change the line above because arr1[i] is undefined
          // you can use indexOf to get the correct index for
          // the item in arr1
          
         //then just update the quantity
         // here's where you'd use indexOf
        
        } else {
          arr1.push(arr2[j]);
        // add the item with its quantity from arr2
        }
      }
    
    return arr1;
}
  1. This is similar to how I did it. I assume there’s a better way.
  2. Just a reminder that even after you get that if-clause figured out, you’ll still have a bunch of failing tests because you need to return the results sorted alphabetically.

#5

So I figured it out:

arr1.forEach(function(item){
            if(item.indexOf(arr2[j][1]) > -1){
              item[0] += arr2[j][0];
}

Not the best solution, but it passed all the tests. Here is the full code:

 function Comparator(a, b) {
   if (a[1] < b[1]) return -1;
   if (a[1] > b[1]) return 1;
   return 0;
 }

function updateInventory(arr1, arr2) {
  var itemsFromArr1 = [];
    for(var i = 0; i < arr1.length; i++){
      itemsFromArr1.push(arr1[i][1]);
    }
    // stop the first for loop here. You want to make sure *all*
    // of the items from arr1 are in that itemsFromArr1 
    // before you start calling `includes` on it.
    
    //now, iterate through arr2 and check if each item is already in itemsFromArr1
      for(var j = 0; j < arr2.length; j++){
        if (itemsFromArr1.includes(arr2[j][1])) {
          arr1.forEach(function(item){
            if(item.indexOf(arr2[j][1]) > -1){
              item[0] += arr2[j][0];
            }
          })
        } else {
          arr1.push(arr2[j]);
        // add the item with its quantity from arr2
        }
      }
    
    return arr1.sort(Comparator);
}

// 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);

#6

Looks good. I think that this:

Could be changed to something along the lines of:

          var i = itemsFromArr1.indexOf(arr2[j][1]);
          arr1[i][0] += arr2[j][0];

You could then avoid that for loop. It trades on the fact that the items in arr1 and itemsFromArr1 share the same order.