My problem ist that my array wont get updated soe the number doesnt get changed. It pushes elements from the second array, that weren’t in the first array but it doesnt update the number of an already existing element (e.g. [21, Apple] is in the first array and [40, Apple] in the second so in the first array I’d need to change it to 40 too)
Add the following console.log statement before the first if statement. I don’t think you understand what is going on with the code you have written.
Also, instructions want to you to add the new inventory to the existing inventory. You are attempting to replace it in your second if statement, though you have some other logic issues in that section you need to figure out.
For example, what is the purpose of the following line of code?
if (arr2[i][0] > arr1[j][0] && arr2[i][1] == arr2[j][1]) {
I solved the exercise now with (almost) the same code now.
I thought first that you’d need to compare the two same elements with their quantities but just realized i need to add the quantity. But basically what I did was, check if the quantities differs from each other and add them together if it’s true. I just realized I could just instantly use the arr1[j] += arr2[i] command after every iteration when arr2[i][1] == arr2[j][1]
The reason why I only check if arr1 is empty is because, if arr2 is empty, nothing gets pushed to arr1 and nothing needs to be pushed.
I’m not really satisfied with my solutions, can I optimize my code more or should I just rewrite it?