Can't merge sub-arrays in Inventory Update

Can't merge sub-arrays in Inventory Update
0

#1

I am stuck figuring out how to merge similar sub-arrays. I have concatenated arr1 and arr2, and alphabetized it. However, I can’t figure out how to merge similar sub-arrays (i.e. the two that say “Bowling Ball”) and add their quantities together. Here is what I have so far:

function updateInventory(arr1, arr2) {
   var allInv=arr1.concat(arr2);
   var alpha= allInv.sort(function(a, b){return b[1] < a[1];});       
      for (var i=0;i<alpha.length;i++){
         for (var j=0;j<alpha[i].length;j++){
            if (alpha[j].indexOf(alpha)){
               
   }
  }
  }      
      
}

Any suggestions?


#2

It sometimes helps to think in plain English, make it easy to understand and write your algorithm from there.
What are we doing here? Adding new products to a stock. How would you do that in real life?

For each new product see if a product with same name is already in stock.
If so then add stock numbers.
If not, add product to arr1.

Sort arr1 by product name.

return arr1.

Now you’ve got a basic algorithm it’s up to you to implement it.

gl;hf :slight_smile: