Inventory Update forEach return many times

Inventory Update forEach return many times
0.0 0

#1

Hi everyone!
I’m having some issues with my code, and I’m trying to understand why forEach pushes all the arr2 plus more when it’s kind of shouldn’t…?? :thinking: or should??

Here is the output without sort:

   [[88, "Bowling Ball"], [ 2, "Dirty Sock"], [3, "Hair Pin"], [5, "Microphone"], [2, "Hair Pin"], [3, "Half-Eaten Apple"], [7, "Toothpaste"], [2, "Hair Pin"], [3, "Half-Eaten Apple"], [67, "Bowling Ball"],  [7, "Toothpaste"],  [3, "Half-Eaten Apple"], [67, "Bowling Ball"], [7, "Toothpaste"],  [2, "Hair Pin"],  [3, "Half-Eaten Apple"],  [67, "Bowling Ball"],  [7, "Toothpaste"]]

Your code so far

function updateInventory(arr1, arr2) {
    // All inventory must be accounted for or you're fired!
  
  arr1.forEach(function(elem){
    arr2.forEach(function(item){
      if(elem[1]!==item[1]){
        arr1.push(item);
      }else{
        elem[0]+=item[0];
      }
      
    });
    
  });
  //console.log(arr1);
  return arr1.sort(function(a, b){
    return a[1] > b[1];
  });
}

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

Your browser information:

Your Browser User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_13_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36.

Link to the challenge:


#2

Because of the way you are looping. You are looping and comparing every item to every other item and if they don’t match you are pushing - your algorithm is flawed. Think about the process. You look at the first item in arr2 - when deciding to add it to arr1, you have to find if that item is anywhere in arr1, not just if it is the first item. So, you have to go down arr2 and find a way to find out if that item exists anywhere in arr1 and base your action on that.

If this doesn’t make sense, get out some scratch paper and do it manually.


#3

It took me few days to understand what you have said)) And yesterday I came up with this:

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

But than it wasn’t passing as well of course)) I was even putting the push in if else statement but no luck… Finally could’t handle and decided to look at the spoilers :persevere: I GAVE up basically… Now I see that I could use flags which I never used before :worried:

function updateInventory(arr1, arr2) {
    // All inventory must be accounted for or you're fired!
  var i = 0;
  arr2.forEach(function(item){
    i = 0;
    arr1.forEach(function(elem){
      if(elem.indexOf(item[1])>-1){
        elem[0]+=item[0];
        i = 1;
      }
        
    });
    
    if(i===0){
      arr1.push(item);
    }
    
  });
  
  return arr1.sort(function(a, b){
    return a[1] > b[1];
  });
}

What could I use instead of this i=0 flag?


#4

With regards to the i=0, this is just a two-state flag, right? Doesn’t that sounds like a boolean? I would do something like:

var isFound = false

#5

So TRUEEE!!! Thanks :crazy_face:
I really forgot about it))