How does the inventory comparison function work?

The every method traverses the array and tests every element of the array.
The Syntax section of the documentation for every shows the various ways this method can be called. In the expression mentioned by you, the syntax is of an arrow function with a single parameter. The syntax is of the form,

every((element) => { /* … */ } )
order.every(item => inventory[item[0]] >= item[1]);

Since item is the only parameter being provided to the arrow function, so each element of the array will be assigned to item and then the element will be tested.
Specifically for the example under discussion:

  • In the first iteration, item will be ["sunglasses", 1], so item[0] will be "sunglasses" and item[1] will be 1

  • In the second iteration, item will be ["bags", 2], so item[0] will be "bags" and item[1] will be 2

order is an array of arrays (i.e. of the form [ [], [], ... ]
In order[0][1], the [0] notation targets the first array (i.e the whole array ["sunglasses", 1]). and the [1] notation targets the second element of that array (i.e. the integer 1).

But, in the every method, the current element being processed will automatically be assigned to the parameter. In our case, the elements of our order array are arrays, so each array will be automatically assigned to item. We don’t have to target the arrays ourselves. That work is done for us.


I appreciate your time and response, now I understand how every method works in a multidimensional array.

What a great question, and what a great answer. Had exactly the same question and @tensor2 answered it perfectly. Thank you community.

Thank you for this answer! The translation of code where you replaced each term with the value it represented was incredibly helpful. Vey much appreciated! :smiling_face:

Very helpful explanation, much appreciated!

Thank you. In this exercise, I was stumped not by Success and Failure Callback Functions, which they are trying to teach us, but by inventory[item[0]]!

inventory[item[0]] = inventory[‘bags’]

We’re just used to using a record like: inventory.bags

So, inventory[‘bags’] = inventory.bags