Https://www.codecademy.com/courses/introduction-to-javascript/lessons/promises/exercises/onfulfilled-onrejected

https://www.codecademy.com/courses/introduction-to-javascript/lessons/promises/exercises/onfulfilled-onrejected

Hello, i want to ask a question regarding the exercise mentioned in the link.

In the following function from the library.js we have:

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]
            //console.log(inventory[item[0]])
            //console.log(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);
    })
};

module.exports = { checkInventory };

and in the app.js we have:

onst {checkInventory} = require('./library.js');

const order = 
[
  ['sunglasses', 20], 
  ['bags', 30]
];

// Write your code below:

let handleSuccess = (resolved) => {
  console.log(resolved);
}

let handleFailure = (rejected) => {
  console.log(rejected);
}

checkInventory(order).then(handleSuccess, handleFailure)

so as far as i understand

order.every(item => inventory[item[0]] >= item[1]
            //console.log(inventory[item[0]])

will translate basically to
1900 >= 20 (this is true)
1900 >= 30 (this is true)

What i tried is to change the first value in the inventory object to sunglasses: 25,,
but when i check this, it also translates to true.

As far as i understand, we should have this to translate to false, since 25 is bigger than the first value of the first item in the order array, but smaller than the first value of the second item of the order array.

Any thoughts?

Welcome to the forums!

From looking over the code, you can still safely order 25 sunglasses so I think the behaviour you’re seeing is right.

This line:
order.every(item => inventory[item[0]] >= item[1])
is checking that you have stock for each item in the order, so that you have at least 20 sunglasses and 30 bags. (You have 1900 and 1344 of each, respectively.)

You’re not comparing each item of the order to each other, but each item in the order to the inventory.

You could add some calls to console.log() to see how the callback function inside of .every() is evaluating your conditions. :slight_smile:

Yes, my question was the following:
If we have in inventory a change like this:

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

and in order it stays the same

> const order = 
> [
>   ['sunglasses', 20], 
>   ['bags', 30]
> ];

When we run the following code:

order.every(item => inventory[item[0]] >= item[1]

It still translates to true which is confusing to me.
Shouldn’t this translate to false?

Yes, my question was the following:
If we have in inventory a change like this:

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

and in order it stays the same

> const order = 
> [
>   ['sunglasses', 20], 
>   ['bags', 30]
> ];

When we run the following code:

order.every(item => inventory[item[0]] >= item[1]

It still translates to true which is confusing to me.
Shouldn’t this translate to false?

No, in short.

If this is confusing you, I would encourage you to replicate the functionality of that line of code by hand. Here are the docs for the every iterator which you can refer to.

Once you’ve done this, the answer to your question should become quite clear. :slight_smile:

Sorry, i forgot to input my changes in the last comment.

The question is the following:

If we have in inventory a change like this:

const inventory = {
    sunglasses: 25,
    pants: 1088,
    bags: 1344
};

and in order it stays the same

> const order = 
> [
>   ['sunglasses', 20], 
>   ['bags', 30]
> ];

When we run the following code:

order.every(item => inventory[item[0]] >= item[1]

It still translates to true which is confusing to me.
Shouldn’t this translate to false?

No, it is correctly evaluating to true.

What do you think that call to .every() is doing? If you’re unsure, perhaps you should add a call to console.log into the callback function so you can watch it?