# Algorithms: Inventory Update

**URL:** <https://forum.freecodecamp.org/t/algorithms-inventory-update/444032>\
**Category:** Curriculum Help\
**Tags:** javascript\
**Created:** [February 2, 2021, 7:46pm UTC](https://forum.freecodecamp.org/t/algorithms-inventory-update/444032 "2021-02-02T19:46:06Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![santiagodeleondecar1](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/santiagodeleondecar1/32/134981_2.png) [@santiagodeleondecar1](https://forum.freecodecamp.org/u/santiagodeleondecar1)\
**Post date:** [February 2, 2021, 7:46pm UTC](https://forum.freecodecamp.org/t/algorithms-inventory-update/444032/1 "2021-02-02T19:46:06Z")

</div>

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**

```js

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

> **[freeCodeCamp.org](https://www.freecodecamp.org/learn/coding-interview-prep/algorithms/inventory-update)**
>
> Learn to Code — For Free

---

<div class="post-metadata">

**Author:** ![ArielLeslie](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/arielleslie/32/264841_2.png) [@ArielLeslie](https://forum.freecodecamp.org/u/ArielLeslie)\
**Post date:** [February 2, 2021, 7:50pm UTC](https://forum.freecodecamp.org/t/algorithms-inventory-update/444032/2 "2021-02-02T19:50:23Z")

</div>

Why are you using global variables?

---

<div class="post-metadata">

**Author:** ![santiagodeleondecar1](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/santiagodeleondecar1/32/134981_2.png) [@santiagodeleondecar1](https://forum.freecodecamp.org/u/santiagodeleondecar1)\
**Post date:** [February 2, 2021, 8:05pm UTC](https://forum.freecodecamp.org/t/algorithms-inventory-update/444032/3 "2021-02-02T20:05:58Z")

</div>

> [@santiagodeleondecar1](#):
>
> ```auto
> 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);
> 
> ```

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

---

<div class="post-metadata">

**Author:** ![santiagodeleondecar1](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/santiagodeleondecar1/32/134981_2.png) [@santiagodeleondecar1](https://forum.freecodecamp.org/u/santiagodeleondecar1)\
**Post date:** [February 2, 2021, 8:06pm UTC](https://forum.freecodecamp.org/t/algorithms-inventory-update/444032/4 "2021-02-02T20:06:55Z")

</div>

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

```auto

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

```

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [February 2, 2021, 8:07pm UTC](https://forum.freecodecamp.org/t/algorithms-inventory-update/444032/5 "2021-02-02T20:07:24Z")

</div>

> [@santiagodeleondecar1](#):
>
> ```auto
> 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"]]
> ;
> 
> ```

Why are these _inside_ your function now?

---

<div class="post-metadata">

**Author:** ![santiagodeleondecar1](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/santiagodeleondecar1/32/134981_2.png) [@santiagodeleondecar1](https://forum.freecodecamp.org/u/santiagodeleondecar1)\
**Post date:** [February 2, 2021, 8:07pm UTC](https://forum.freecodecamp.org/t/algorithms-inventory-update/444032/6 "2021-02-02T20:07:28Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![santiagodeleondecar1](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/santiagodeleondecar1/32/134981_2.png) [@santiagodeleondecar1](https://forum.freecodecamp.org/u/santiagodeleondecar1)\
**Post date:** [February 2, 2021, 8:08pm UTC](https://forum.freecodecamp.org/t/algorithms-inventory-update/444032/7 "2021-02-02T20:08:16Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [February 2, 2021, 8:08pm UTC](https://forum.freecodecamp.org/t/algorithms-inventory-update/444032/8 "2021-02-02T20:08:42Z")

</div>

Well, what error are you getting from this code?

---

<div class="post-metadata">

**Author:** ![santiagodeleondecar1](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/santiagodeleondecar1/32/134981_2.png) [@santiagodeleondecar1](https://forum.freecodecamp.org/u/santiagodeleondecar1)\
**Post date:** [February 2, 2021, 8:10pm UTC](https://forum.freecodecamp.org/t/algorithms-inventory-update/444032/9 "2021-02-02T20:10:32Z")

</div>

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

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

```

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [February 2, 2021, 8:11pm UTC](https://forum.freecodecamp.org/t/algorithms-inventory-update/444032/10 "2021-02-02T20:11:11Z")

</div>

That version passes for me.

---

<div class="post-metadata">

**Author:** ![santiagodeleondecar1](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/santiagodeleondecar1/32/134981_2.png) [@santiagodeleondecar1](https://forum.freecodecamp.org/u/santiagodeleondecar1)\
**Post date:** [February 2, 2021, 8:13pm UTC](https://forum.freecodecamp.org/t/algorithms-inventory-update/444032/11 "2021-02-02T20:13:17Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![JeremyLT](https://sea3.discourse-cdn.com/freecodecamp/user_avatar/forum.freecodecamp.org/jeremylt/32/291894_2.png) [@JeremyLT](https://forum.freecodecamp.org/u/JeremyLT)\
**Post date:** [February 2, 2021, 8:14pm UTC](https://forum.freecodecamp.org/t/algorithms-inventory-update/444032/12 "2021-02-02T20:14:17Z")

</div>

> [@santiagodeleondecar1](#):
>
> ```auto
> 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);
> 
> ```

Using exactly and only the code above yields

 ![image](https://global.discourse-cdn.com/freecodecamp/original/3X/7/5/7598a2ea626742bf1780427f9bb11b8640a12d80.png)

---

<div class="post-metadata">

**Author:** ![system](https://global.discourse-cdn.com/freecodecamp/original/3X/e/1/e1c433102e34b46dfaa3e15d13ab9a514b127078.svg) [@system](https://forum.freecodecamp.org/u/system)\
**Post date:** [August 4, 2021, 8:14am UTC](https://forum.freecodecamp.org/t/algorithms-inventory-update/444032/13 "2021-08-04T08:14:41Z")

</div>

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.
