Inventory Update Error- Help

Hello FCC camper,

I’m working on Inventory Update. My solution kept showing me the error “TypeError: Cannot read property ‘1’ of undefined”. I checked other threads but I couldn’t figure it out by myself…

Here is my code.

 function updateInventory(arr1, arr2) {
   // All inventory must be accounted for or you're fired!
 var newArr = [];
   // push arr1 to a new array
 for(var i=0; i<arr1.length; i++){
 newArr.push(arr1[i]);
}
// loop through arr2 and check
for(var j=0; j<newArr.length; j++){
// if there is no same value, push it to a new array
if(newArr.indexOf(arr2[j][1]) <0){
 newArr.push(arr2[j]);
// if same value already exist, add the inventory to the current one. 
  } else if (newArr[j][1] === arr2[j][1]){
 newArr[j][1] += arr2[j][1];
 } }
   // change to alphabetical order 
 var result = newArr.sort(function(a,b){
return a[1]>b[1];
});
return result;
}

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

If you can give me any comments or feedback, I will be super happy.
Thank you so much in advance.

This is what I found: In the second for-loop, you intended to loop through arr2 using newArr's length. In one example, both arrays have the same length. But then you’re pushing items to newArr, increasing its length. So now newArr is larger than arr2, and because the condition in the loop compares to newArr.length, j can have values way past arr2's length, so you get arr2[j] is undefined.

1 Like

Thank you so much for taking time to look into my code…!
I followed your advice and I could solve that issue.
But still I cannot pass the test as I cannot print out correct numbers in a new array…

 function updateInventory(arr1, arr2) {
// All inventory must be accounted for or you're fired!
var newArr = [];
// push arr1 to a new array
for(var i=0; i<arr1.length; i++){
newArr.push(arr1[i]);
}

// loop through arr2 and check
 for(var j=0; j<arr2.length; j++){
// if there is no same value, push it to a new array
if(arr1.indexOf(arr2[j][1]) <0){
 newArr.push(arr2[j]);
// if same value already exist, add the inventory to the current one. 
 } else if (arr1[i][1] === arr2[j][1]) {
 arr1[i][0] += arr2[j][0];
} 
}

// change to alphabetical order 
var result = newArr.sort(function(a,b){
return a[1]>b[1];
});
return result;
 }


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

I see a problem here:

.indexOf here will always return -1 because arr1 is an array of arrays, but arr2[j][1] is always a string, so you can’t find it in arr1 and will always get -1. You’ll have to come up with a way to check if there’s an item in arr1 that has the same name as that of arr2[j].

You’ll have to come up with a way to get the index of the item you want to update and use that instead of i, because i was only used in the first for-loop and you can’t just plug it in here, or you’ll get an error. Also, I think you want to update newArr instead of arr1.

1 Like

Thank you for your explanation.
I was thinking of solution for another hour. In the end I pushed all objects directly to newArr.
This problem helps me to understand how to use loop correctly better… I think.

Thanks for your help, again!!!