Inventory Update: how to use nested-for loops

so I am able to get the first part of the problem with the code below where if a newInv item already exists in curInv, we just update the stock.

However, when I put an else statement so that the items in newInv that are not in curInv are pushed into curInv, it does not run properly.

I’m having a hard time understanding how to solve this second part of the problem using a nested for-loop.

Is there a way to solve this second part using a nested for-loop like the one I have below? If not, whynot? I’m trying to gain a better understanding of how nested-for loops work and when to use them. Thanks in advance.

Your code so far

for (let i = 0 ; i < curInv.length; i++){
	for (let j = 0 ; j < newInv.length; j++){
		if (curInv[i][1] === newInv[j][1]){
			curInv[i][0] += newInv[j][0]
		} else {
			curInv.push(newInv[j])
		} 
	}
}

// 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:

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

Link to the challenge:
https://learn.freecodecamp.org/coding-interview-prep/algorithms/inventory-update/

You may want to try again the logic of your loops.
Let’s try to break it down.
With i = 0, you have "Bowling Ball" item.
So with j=0 you have "Hair Pin"
The if statements say that if those two are different, than "Hair Pin" is pushed to curInv.
With j = 1 is "Half Eaten Apple", it is still different so it is pushed there.
[…]
Now, let’s try with
i = 1, there is "Dirty Socks".
At j = 0 there is "Hair Pin", t is different than "Dirty Socks" so "Hair Pin"' is pushed to 'curInv

etc

Is this what you want?

There is a problem in the logic inside loop. You are adding the new value every time if it doesn’t match? What if it will match something later on? Put in some console.log statements and step through it. See if you can come up with a better solution.

ah ok. So that is not what I want.

What I want to do is check to see if there are any Items in newInv that are not in curInv. So I guess I would have to loop thru newInv and check if each item in newInv exists in any of the arrays inside curInv.

The code below properly adds the missing items to curInv but it ends up doubling the stock.

Could you help me understand the flow of this logic and why it’s doubling on top of pushing the newInv[j] ?

function updateInventory(curInv, newInv) {
    for (let i = 0 ; i < curInv.length; i++){
	for (let j = 0 ; j < newInv.length; j++){
		if (curInv[i][1] === newInv[j][1]){
			curInv[i][0] += newInv[j][0]
		} else if (curInv.every((item)=>item[1] !== newInv[j][1])){
			curInv.push(newInv[j])
        } 
	}
}
    return curInv
}

Do you need to do this check for every j? It requires a lot of resources and you do it multiple times unneccessary in that way. See the execution of your code here for example: https://goo.gl/V399b5

if (curInv.every((item)=>item[1] !== newInv[j][1])){
            curInv.push(newInv[j])}

The only thing you are missing is the alphabetical order of the returned elements.
From the challenge instructions:

The returned inventory array should be in alphabetical order by item.