Access Property Names with Bracket Notation again

Tell us what’s happening:

Try as I might after RAS I can’t seem to make hide nor hair of this. Can anyone point me in the right direction?

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


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

// 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.99 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/

Ah man, you’re there! The only problem is that you used the wrong parentheses: the bracket notation is the one used in the example ( the one you reported above your return statement which you should get rid of ) and that’s the syntax to dynamically check properties of an object ( e.g. using a variable with different values to read different properties )

Good luck!

1 Like

But the challenge says I need a return statement don’t I?

I’m also not sure where SelectedFoods comes from since it wasn’t defined anywhere else in the functions.

I pasted the solution here but its not passing the challenge. Access Property Names with Bracket Notation2

Yes yes, the second line is the almost correct one ^^
The first line is an example you have to get rid of, just watch how it use the parenthesis before to delete it and reply to syntax in your line^^

EDIT:
Ah, just clicked the link: read the comment @JM-Mendez put into his example :wink:

This is my code and it works fine

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[scannedItem]:undefined;
}

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

Here is the solution:

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

@Temiogundeji and @Lebubblez In the future, instead of posting the solution, offer hints and suggestions to the OP. @Cyath merely wanted some guidance with the solution and not the complete solution. Another option is to give examples of your hints and suggestions.

It’s ok I learn from reverse enginnering the solution. Many thanks!