Success and Failure Callback Functions: i dont get the every method

Question
I was working on the exercise(https://www.codecademy.com/paths/full-stack-engineer-career-path/tracks/fscp-22-async-javascript-and-http-requests/modules/wdcp-22-learn-javascript-syntax-promises/lessons/promises/exercises/onfulfilled-onrejected), the thing is that i dont get what does item do on order.every method, library.js.

Code
library.js

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

module.exports = { checkInventory };

app.js

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

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

// Write your code below:
const handleSuccess = (resolveValue) =>{
  console.log(resolveValue);
}

const handleFailure = (rejectionReason) =>{
  console.log(rejectionReason);
}

checkInventory(order)
  .then(handleSuccess, handleFailure);

help me, pls

The .every() method checks whether every item in an array passes some test provided in the proceeding callback function. In the case of library.js, it checks whether every item quantity provided in the order array is less than or equal to the total units left in the inventory object.

For example, if I provide the following order, it will return true because the order numbers are less than the inventory numbers:

let order = [['sunglasses', 100], ['pants', 200]]

However, if I provide this order, then it will return false because at least one of the order numbers is greater than the corresponding inventory numbers:

let order = [['sunglasses', 100], ['pants', 1200]]

Even though my order for sunglasses didn’t change, one of the order numbers exceeded the inventory number, so the .every() method would return false because every element in the array has to pass to return true.

3 Likes

Not sure what you mean.

nvm, i was comparing the results of the every().
i said that is like:

item => inventory[item[0]] >= item[1]

inventory.sunglasses >= order[0][1];

both have the same result
btw thank you for help me understand, the every method