• Guest, before posting your code please take these rules into consideration:
    • It is required to use our BBCode feature to display your code. While within the editor click < / > or >_ and place your code within the BB Code prompt. This helps others with finding a solution by making it easier to read and easier to copy.
    • Don't share a wall of code. All we want is the problem area, the code related to your issue.


    To learn more about how to use our BBCode feature, please click here.

    Thank you, Code Forum.

JavaScript .every method with function

KISHOR 1988

New Coder
JavaScript:
const inventory = {
  sunglasses: 1900,
  pants: 1088,
  bags: 1344
};
debugger;
const order = [['sunglasses', 1], ['bags', 500]];

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


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

const handleFailure = (n) => {
  console.log(n)
};
checkInventory(order).then(handleSuccess,handleFailure);




i am beginner so please explain line number 12 that contains ---- item => inventory[item[0]] >= item[1]------what is the value of "item" on every step and how it works??? please break down line number 12 and "item" . i spent two hours for break down but i cant. i also try debugger but i cant understand.
 

Antero360

Active Coder
JavaScript:
const inventory = {
  sunglasses: 1900,
  pants: 1088,
  bags: 1344
};
debugger;
const order = [['sunglasses', 1], ['bags', 500]];

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


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

const handleFailure = (n) => {
  console.log(n)
};
checkInventory(order).then(handleSuccess,handleFailure);




i am beginner so please explain line number 12 that contains ---- item => inventory[item[0]] >= item[1]------what is the value of "item" on every step and how it works??? please break down line number 12 and "item" . i spent two hours for break down but i cant. i also try debugger but i cant understand.
Hey there,

lol, don't worry about how long you take to research a particular question. The longer you take, the better you become at sifting through the noise in order to get your answer :) now in regard to line 12:

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

so.. lets start at the beginning. "order" is an array of "items". The .every() method, will iterate through all the items in "order" array. "item" is a variable that gets filled when the following criteria gets met: the value of the current item (item[0]) in the inventory array is greater or equal to the value of the quantity being ordered (item[1]). Once this criteria gets met, item becomes filled with that "item" data. This item then gets added to another array called inStock, to show what items are currently in stock
 

Top