ZhichengChen 14bfa61cea
fix(i18n): update Chinese translation of basic data structures (#38054)
Co-authored-by: Zhicheng Chen <chenzhicheng@dayuwuxian.com>
2020-08-04 12:44:21 +05:30

3.1 KiB
Raw Blame History

id, title, challengeType, forumTopicId, localeTitle
id title challengeType forumTopicId localeTitle
587d7b7c367417b2b2512b1a Access Property Names with Bracket Notation 1 301150 使用方括号访问属性名称

Description

在关于对象的第一个挑战中,我们提到可以在方括号符号中用一个变量作为属性名来访问属性值。假设一个超市的收银台的程序中使用了一个foods对象,并且有一些程序逻辑会设置selectedFood,我们需要查询foods对象来检查某种食物是否存在,我们可以这样写检查逻辑:
let selectedFood = getCurrentFood(scannedItem);
let inventory = foods[selectedFood];

上述代码会先计算selectedFood变量的值,并返回foods对象中以该值命名的属性对应的值,若没有以该值命名的属性则会返回undefined。有时候对象的属性名在运行之前是不确定的,或者我们需要动态地访问对象的属性,这时方括号符号就会很有用。

Instructions

我们已经定义了一个checkInventory函数,它接受一个被扫描到的商品名作为输入参数。它要返回foods对象中以scannedItem的值命名的属性的值。只有有效的属性名会作为参数传入checkInventory,你在完成挑战时不需处理参数无效的情况。

Tests

tests:
  - text: <code>checkInventory</code>是一个函数
    testString: assert.strictEqual(typeof checkInventory, '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});'
  - text: <code>checkInventory(&quot;apples&quot;)</code>应该返回<code>25</code>
    testString: assert.strictEqual(checkInventory('apples'), 25);
  - text: <code>checkInventory(&quot;bananas&quot;)</code>应该返回<code>13</code>
    testString: assert.strictEqual(checkInventory('bananas'), 13);
  - text: <code>checkInventory(&quot;strawberries&quot;)</code>应该返回<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
};
// 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
let foods = {
  apples: 25,
  oranges: 32,
  plums: 28,
  bananas: 13,
  grapes: 35,
  strawberries: 27
};

function checkInventory(scannedItem) {
  return foods[scannedItem];
}