Doubt about variable - javascript

Hello everyone,

I was working on premises when I found this script.

const order = [['sunglasses', 4], ['bags', 2]];

const inventory = {
    sunglasses: 1900,
    pants: 1088,
    bags: 1344,

const checkInventory = (order) => {
    return new Promise((resolve, reject) => {
        setTimeout(() => {
            **let inStock = order.every(item => inventory[item[0]] >= item[1]);**
            if (inStock) {
                resolve('Thank you. Your order was successful.');
            } else {
                reject(`We're sorry. Your order could not be completed because some items are sold out.`);
        }, 1000);

I highlighted in bold the piece of code that I do not understand.

I get that inStock is a boolean var which will have either true or false.
The value is based on the result of the iteration done on ‘inventory’ and if all of the inventory keys have a value greater than the value in ‘order’, inStock will be True.

let inStock = order.every(item => inventory[item[0]] >= item[1]);

I try to translate what I understand from this piece of code:
for every item in order (order.every(item =>), check if that item in inventory (inventory[item[0]] - why the [0]?) is bigger or equal than the value of that item in order (item[1]).

Honestly I do not get the indexing. Why inventory[item [0]] and item [1]?

Thanks in advance,

an order has multiple items. Each item consist of two things, the item and the amount of this item the customer would like to purchase:

['sunglasses', 4]

see a connection between 0 and 1 and this list?

Also, express what has to happen in plain English. Understanding what the code is suppose to do really helps

in this case, we want to check we have enough items in stock that the user/customer wants to buy.

1 Like

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