# Inventory Update - Help me with my code

Inventory Update - Help me with my code
0

#1

How I intend my code to work is all explained in the code itself. So let’s focus on the problem. In the loop part only the index of the first item is being used with all quantities. In the current case, 67 is being added to all quantities values and that’s not what I want of course. The value 67 should be added only to 21, for example. My code isn’t complete yet because I need to make the loop to work properly.

``````function updateInventory(arr1, arr2) {

// Create two different arrays to store curInv's quantity and name of the items.
var cuQuantity = [];
var cuItem = [];
for (var i = 0; i<arr1.length;i++){
cuQuantity.push(arr1[i][0]);
cuItem.push(arr1[i][1]);

}

// Create two different arrays to store newInv's quantity and name of the items.
var newQuantity = [];
var newItem = [];
for (var j = 0; j<arr2.length;j++){
newQuantity.push(arr2[j][0]);
newItem.push(arr2[j][1]);
}

var index = ""; // Temporary store the correspondent indexes of newItem in cuItem
var cuQup = []; // Store all the updated quantities after the loop

//Find all the correspondent indexes of newItem in cuItem
for (var k = 0; k<newItem.length;k++){
if (cuItem.indexOf(newItem[k] !== -1)){
index = cuItem.indexOf(newItem[k]);
//Update all the quantities and store them in cuQup
for (var l = 0; l<cuQuantity.length;l++){
cuQuptemp = cuQuantity[l] + newQuantity[index];
cuQup.push(cuQuptemp);
}
//If an item can't be found
} else {
cuItem.push(newItem[index]);
cuQup.push(newQuantity[index]);
}
}

// Join cuQup and cuItem in a single array
var join = [];

}
// 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 User Agent is: `Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:56.0) Gecko/20100101 Firefox/56.0`.

#2

I believe one of the first problems I see is you have a closing parentheses in the wrong place below.
You have:

``````  for (var k = 0; k<newItem.length;k++){
if (cuItem.indexOf(newItem[k] !== -1)){
``````

I think you meant:

``````  for (var k = 0; k<newItem.length;k++){
if (cuItem.indexOf(newItem[k]) !== -1){
``````

Second, in the following code you update the current quantity and assign it to cuQuptemp.

``````for (var l = 0; l<cuQuantity.length;l++){
cuQuptemp = cuQuantity[l] + newQuantity[index];
cuQup.push(cuQuptemp);
}
``````

This is fine, but why do you push a new value into cuQup? These are quantities that already exist in the correct position of cuQup. You just need to replace the current value like the following code does:

``````cuQup[l] = cuQuptemp;
``````

This does not solve all of your logic issues, because you still end up with a duplicate ‘Bowling Ball’ item in cuItem and are missing Half-Eaten Apple’ and ‘Toothpaste’, but this will hopefully get you closer to a solution. You just need to add some console.log statement in various places so you can see what each array has in it after some changes (push or replace of a value) and you should be able to figure out the rest.