Access Property Names with Bracket Notation - question

Hi, I resolved following task but it took me a while, my question is how does it know that
foods[’’] is foods[scannedItem]?
I wanted to use this syntax foods[’’]=foods[scannedItem] and tried to loop it until foods[’’] <= foods.strawberry but it never worked. Could you explain this to me please?
Thank you

Your code so far


let foods = {
apples: 25,
oranges: 32,
plums: 28,
bananas: 13,
grapes: 35,
strawberries: 27
};

function checkInventory(scannedItem) {
// Only change code below this line
return foods[scannedItem];
};
// Only change code above this line


console.log(checkInventory("apples"));


Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:75.0) Gecko/20100101 Firefox/75.0.

Challenge: Access Property Names with Bracket Notation

Link to the challenge:

can you show an example of this?

It doesn’t know that

are you asking where the value of scannedItem come from?

that is from the function call

2 Likes

all right, so without function call it would have to be done in my way? if it is possible first of all.
I tried something like that but it does not pass this condition: The foods object should have only the following key-value pairs: apples: 25 , oranges: 32 , plums: 28 , bananas: 13 , grapes: 35 , strawberries: 27 . The function returns correct values though.

function checkInventory(scannedItem) {
let y = 0;
while (y<6){
foods[’’]=foods[scannedItem]
y++;
}
return foods[scannedItem];
};

foods[''] is not a meaningful expression. What do you think it means?

Well, I think it can match any keys in the object foods.

Not really. foods[''] doesn’t mean anything. It’s not valid JavaScript syntax - You get a syntax error when you try to do anything with that code.

1 Like

ok thanks for claryfiing this to me

1 Like