Access Property Names with Bracket Notation2

Tell us what’s happening:

What would be the solution ?

Your code so far


let foods = {
  apples: 25,
  oranges: 32,
  plums: 28,
  bananas: 13,
  grapes: 35,
  strawberries: 27
};
// do not change code above this line

function checkInventory(scannedItem) {
  // change code below this line
  return (scannedItem = foods);
}

// change code below this line to test different cases:
console.log(checkInventory("apples"));

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-data-structures/access-property-names-with-bracket-notation/

let inventory = foods[selectedFood];
This line on top is your guide to the solution. You can retrieve any values from an object using syntax of

obj[key]

Therefore foods[selectedFood] will return any value (if there is any) that has a key of whatever you pass in as selectedFood in foods object. And you are doing the samething here in the solution.

If you still need help, return foods[scannedItem];

So i did this ,

function checkInventory(scannedItem) {
  // change code below this line
let inventory = foods[selectedFood];
return foods[scannedItem];
}

I still need help

This is the error you get

selectedFood is not defined

What this means is that you are trying to access a variable, function, or property value that does not exist.

Hint: take a look at this line

let inventory = foods[selectedFood]

Ok , So How to define that sleectedFood ?

The real question is why do you need it? I think the example is throwing you off. Take a look at the requirements again.

  1. We’ve defined a function, checkInventory, which receives a scanned item as an argument.
  2. Return the current value of the scannedItem key in the foods object.
  3. You can assume that only valid keys will be provided as an argument to checkInventory.

I am really not getting it at all .

this line is not needed to pass the tests.

let inventory = foods[selectedFood];

the example is throwing you off.

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

Here, they’re showing you how to call the function getCurrentFood with the scannedItem variable. But you don’t have or need the getCurrentFood function.


So here’s what we know about the requirements

  • The function accepts a scannedItem argument.
  • You also have access to the foods object.

You were right to access the property with foods[scannedItem]. Since that’s all that the test requires, you don’t have to define selectedFood. You can just return the actual property.

Ok , still not getting the correct code . Can you please provide what would be the correct answer ? Thanks

function checkInventory(scannedItem) {
  // you don't need this line, so remove it 
  let inventory = foods[selectedFood];
  return foods[scannedItem];
}
1 Like

It’s not working at all . it’s showing

// running test
selectedFood is not defined
selectedFood is not defined
selectedFood is not defined
selectedFood is not defined
selectedFood is not defined
// tests completed

Did you remove the line I told you to in the comment?

Reread the code I posted, and remove the line under the comment.

It won’t work if you just copy paste it.

I got it at last … Thank you

Here is the solution:

let checkInventory = foods[scannedItem];
return foods[scannedItem];