2.9 KiB
2.9 KiB
id, title, challengeType, forumTopicId
id | title | challengeType | forumTopicId |
---|---|---|---|
587d7b7c367417b2b2512b1a | Access Property Names with Bracket Notation | 1 | 301150 |
Description
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. Bracket notation is very useful because sometimes object properties are not known before runtime or we need to access them in a more dynamic way.
Instructions
checkInventory
, which receives a scanned item as an argument. Return the current value of the scannedItem
key in the foods
object. You can assume that only valid keys will be provided as an argument to checkInventory
.
Tests
tests:
- text: <code>checkInventory</code> should be a function.
testString: assert.strictEqual(typeof checkInventory, 'function');
- text: 'The <code>foods</code> object should have only the following key-value pairs: <code>apples: 25</code>, <code>oranges: 32</code>, <code>plums: 28</code>, <code>bananas: 13</code>, <code>grapes: 35</code>, <code>strawberries: 27</code>.'
testString: 'assert.deepEqual(foods, {apples: 25, oranges: 32, plums: 28, bananas: 13, grapes: 35, strawberries: 27});'
- text: <code>checkInventory("apples")</code> should return <code>25</code>.
testString: assert.strictEqual(checkInventory('apples'), 25);
- text: <code>checkInventory("bananas")</code> should return <code>13</code>.
testString: assert.strictEqual(checkInventory('bananas'), 13);
- text: <code>checkInventory("strawberries")</code> should return <code>27</code>.
testString: assert.strictEqual(checkInventory('strawberries'), 27);
Challenge Seed
let foods = {
apples: 25,
oranges: 32,
plums: 28,
bananas: 13,
grapes: 35,
strawberries: 27
};
function checkInventory(scannedItem) {
// Only change code below this line
// Only change code above this line
}
console.log(checkInventory("apples"));
Solution
let foods = {
apples: 25,
oranges: 32,
plums: 28,
bananas: 13,
grapes: 35,
strawberries: 27
};
function checkInventory(scannedItem) {
return foods[scannedItem];
}