3.3 KiB
3.3 KiB
id, title, challengeType, videoUrl, localeTitle
id | title | challengeType | videoUrl | localeTitle |
---|---|---|---|---|
587d7b7c367417b2b2512b1a | Access Property Names with Bracket Notation | 1 | 使用括号表示法访问属性名称 |
Description
foods
对象被用于超市收银机的程序中。我们有一些设置selectedFood
功能,我们想检查我们的foods
对象是否存在该食物。这可能看起来像: let selectedFood = getCurrentFood(scanningItem);此代码将评估存储在
让库存=食物[selectedFood];
selectedFood
变量中的值,并在foods
对象中返回该键的值,如果不存在则返回undefined
。括号表示法非常有用,因为有时候对象属性在运行时之前是未知的,或者我们需要以更动态的方式访问它们。 Instructions
checkInventory
,它接收一个扫描的项目作为参数。返回foods
对象中的scannedItem
键的当前值。您可以假设只有有效键将作为checkInventory
的参数提供。 Tests
tests:
- text: <code>checkInventory</code>是一个函数
testString: 'assert.strictEqual(typeof checkInventory, "function", "<code>checkInventory</code> is a function");'
- text: '<code>foods</code>对象应该只有以下键值对: <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}, "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>");'
- text: <code>checkInventory("apples")</code>应该返回<code>25</code>
testString: 'assert.strictEqual(checkInventory("apples"), 25, "<code>checkInventory("apples")</code> should return <code>25</code>");'
- text: <code>checkInventory("bananas")</code>应该返回<code>13</code>
testString: 'assert.strictEqual(checkInventory("bananas"), 13, "<code>checkInventory("bananas")</code> should return <code>13</code>");'
- text: <code>checkInventory("strawberries")</code>应该返回<code>27</code>
testString: 'assert.strictEqual(checkInventory("strawberries"), 27, "<code>checkInventory("strawberries")</code> should return <code>27</code>");'
Challenge Seed
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
}
// change code below this line to test different cases:
console.log(checkInventory("apples"));
Solution
// solution required