Not Understanding "Access Property Names with Bracket Notation Lesson"

For instance, imagine that our foods object is being used in a program for a supermarket cash register. We have some function that sets the selectedFood and we want to check our foods object for the presence of that food. This might look like:

let selectedFood = getCurrentFood(scannedItem);
let inventory = foods[selectedFood];

This code will evaluate the value stored in the selectedFood variable and return the value of that key in the foods object, or undefined if it is not present

which is the function in this example?
selectedFood corresponds to what?
getCurrentFood corresponds to what?
scannedItem = potato (whichever type of food in the foods object), right?

If you have a link to the section this is from it’d help to look at it for context.

here’s the link https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-data-structures/access-property-names-with-bracket-notation

OK. Thank you.

So in the example:

let selectedFood = getCurrentFood(scannedItem);
let inventory = foods[selectedFood];

The function is getCurrentFood, which takes an argument scannedItem.
The function takes the argument and then searches through an object variable to see if that scannedItem exists as a property.
It basically looks at all the keys of the object to see if it exists, if it does it returns the value associated with the key.

Another example to make see all of this working:

//Inventory is an object of {key: value} pairs
let inventory = {
  apples: 25,
  oranges: 32,
  plums: 28,
  bananas: 13,
  grapes: 35,
  strawberries: 27
};

// we define a function to search through the inventory
function checkInventory(scannedItem) {
  return inventory[scannedItem]
}

//Then we can call that function to actually search:
checkInventory("oranges")

// this function  goes through the inventory finds "oranges" and returns 32

Hope That’s a little clearer.

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.