Algorithms: Inventory Update

I do not understand what is the reason why this not passing every test in FCC. I applied console.log with every test and I got the expected answers

Your code so far


function updateInventory(arr1, arr2) {
 let arrOfInventory = [];
 for(let i = 0;i<arr1.length;i++){
     for(let j= 0; j<arr2.length;j++){
         if(arr1[i][1]===arr2[j][1]){
             arr1[i][0]+=arr2[j][0]
         }
     }
}
  for(let i=0;i<arr1.length;i++){
      arrOfInventory.push(arr1[i][1])

  }
 for(let i=0;i<arr2.length;i++){
     if(!arrOfInventory.includes(arr2[i][1])){
         arr1.push(arr2[i])
          
     }
 }
 function organization(a,b){
     if(a[1]>b[1]){
         return 1;
     }{
      if(b[1]>a[1]){
        return -1; 

         }
     }
 }

  curInv=curInv.sort(organization)
    
  return curInv     
}

// 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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/88.0.4324.96 Safari/537.36.

Challenge: Inventory Update

Link to the challenge:

Why are you using global variables?

1 Like

I just leave them in there. I did not think about erasing them.

I have changed the place, but I still I am getting errors. What could I do?


function updateInventory(arr1, arr2) {
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"]]
;
    
 let arrOfInventory = [];
 for(let i = 0;i<arr1.length;i++){
     for(let j= 0; j<arr2.length;j++){
         if(arr1[i][1]===arr2[j][1]){
             arr1[i][0]+=arr2[j][0]
         }
     }
}
  for(let i=0;i<arr1.length;i++){
      arrOfInventory.push(arr1[i][1])

  }
 for(let i=0;i<arr2.length;i++){
     if(!arrOfInventory.includes(arr2[i][1])){
         arr1.push(arr2[i])
          
     }
 }
 function organization(a,b){
     if(a[1]>b[1]){
         return 1;
     }{
      if(b[1]>a[1]){
        return -1; 

         }
     }
 }

  arr1=arr1.sort(organization)
    
  return arr1   
}

updateInventory(curInv , newInv);

Why are these inside your function now?

1 Like

Thanks for answering. I have not seen your messages in a long time.

I am just trying it. What could I do now?

Well, what error are you getting from this code?

1 Like

It works with every test by using console.log. I do not understand what is the reason why it does not pass the challenge

function updateInventory(arr1, arr2) {

    
 let arrOfInventory = [];
 for(let i = 0;i<arr1.length;i++){
     for(let j= 0; j<arr2.length;j++){
         if(arr1[i][1]===arr2[j][1]){
             arr1[i][0]+=arr2[j][0]
         }
     }
}
  for(let i=0;i<arr1.length;i++){
      arrOfInventory.push(arr1[i][1])

  }
 for(let i=0;i<arr2.length;i++){
     if(!arrOfInventory.includes(arr2[i][1])){
         arr1.push(arr2[i])
          
     }
 }
 function organization(a,b){
     if(a[1]>b[1]){
         return 1;
     }{
      if(b[1]>a[1]){
        return -1; 

         }
     }
 }

  arr1=arr1.sort(organization)
    
  return arr1   
}

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

That version passes for me.

1 Like

Did you pass it with the code? I cannot pass it. What is going on? :frowning:

Using exactly and only the code above yields

1 Like