Welcome!

By registering with us, you'll be able to discuss, share and private message with other members of our community.

SignUp Now!
  • 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.
    • You can also use markdown to share your code. When using markdown your code will be automatically converted to BBCode. For help with markdown check out the markdown guide.
    • 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

[CODE lang="javascript" highlight="12"]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);[/CODE]




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.
 
[CODE lang="javascript" highlight="12"]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);[/CODE]




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
 

New Threads

Latest posts

Buy us a coffee!

Back
Top Bottom