Help me break down this function

onst checkInventory = (order) => {
  return new Promise ((resolve, reject) => {
   setTimeout(()=> {  
   const itemsArr = order.items;  
   let inStock = itemsArr.every(item => store[item[0]].inventory >= item[1]);
   
   if (inStock){
     let total = 0;   
     itemsArr.forEach(item => {
       total += item[1] * store[item[0]].cost
     });
     console.log(`All of the items are in stock. The total cost of the order is ${total}.`);
     resolve([order, total]);
   } else {
     reject(`The order could not be completed because some items are sold out.`);
   }     
}, generateRandomDelay());
 });
};

Hello everyone and good morning,

So, I am not entirely sure , what this function is doing and I would like you to help me understand. This is what I understood so far :

1 - We define a function named " checkInventory" with one parameter called “order”.

2 - we return a promise which will be either resolved or rejected.

3 - Inside this promise we define 2 variables.
One called “itemsArr” which is equal to the items inside the order input, right ?
Second called “inStock” which check if every element inside the variable above is equal or greater to 1 , right ?

But i dont understand exatcly how the code is doing it :

(item => store[item[0]].inventory >= item[1]);

Where is the item value coming from ? And why is the arrow symbol there ?

I know store is the object and that inventory is a propertie of that object , but what does item[0] actually mean and item[1]?

1 Like

Hi,

I’ll try to explain this bit. I’m still learning too but I find trying to help others understand also helps me to understand.

So as you established itemsArr is an array you have created inside the callback of the checkInventory function.

The array is made up of the items given in the order argument of the checkInventory function.

In stock is going to be a boolean, ie true or false as it calls the every method on the items array.

Click here for details on the every method.
The every method will check each item in the array to check it matches the argument given in it’s own callback function which is written in arrow syntax in your example.

item is the name we give to refer to each element being checked. You could call it anything, it just gives a way of referring to each array index. item[0] is used to notate an index relative to whatever item is.

2 Likes

Hey ,

Thanks for the reply.

So , do you think inside the every method are we defining a function aswell ? And is the item “value” the input of that function ( coming from the itemsArr ) written in arrow syntax ?

No worries.

Yes, we are creating an anonymous callback function inside the every method and item is the argument we give it. We use the callback function to to explain what condition we want to check each item of the array against. If all are true then inStock = true and if not then inStock = false

So in this instance we are asking if the following is true or false for all items in itemsArr:
That the inventory property of the given item in the store object is greater than or equal to the amount of that item required passed in the order.

1 Like