Oliver Eyton-Williams ee1e8abd87
feat(curriculum): restore seed + solution to Chinese (#40683)
* feat(tools): add seed/solution restore script

* chore(curriculum): remove empty sections' markers

* chore(curriculum): add seed + solution to Chinese

* chore: remove old formatter

* fix: update getChallenges

parse translated challenges separately, without reference to the source

* chore(curriculum): add dashedName to English

* chore(curriculum): add dashedName to Chinese

* refactor: remove unused challenge property 'name'

* fix: relax dashedName requirement

* fix: stray tag

Remove stray `pre` tag from challenge file.

Signed-off-by: nhcarrigan <nhcarrigan@gmail.com>

Co-authored-by: nhcarrigan <nhcarrigan@gmail.com>
2021-01-12 19:31:00 -07:00

3.2 KiB
Raw Blame History

id, title, challengeType, forumTopicId, dashedName
id title challengeType forumTopicId dashedName
587d7b7c367417b2b2512b18 将键值对添加到对象中 1 301153 add-key-value-pairs-to-javascript-objects

--description--

对象object本质上是键值对key-value pair的集合。或者说,一系列被映射到唯一标识符的数据就是对象;习惯上,唯一标识符叫做属性property或者key);数据叫做value。让我们来看一个简单的例子:

const tekkenCharacter = {
  player: 'Hwoarang',
  fightingStyle: 'Tae Kwon Doe',
  human: true
};

上面的代码定义了一个叫做 tekkenCharacter 的“铁拳”游戏人物对象。它有三个属性,每个属性都对应一个特定的值。如果我们想为它再添加一个叫做 origin 的属性,可以这样写:

tekkenCharacter.origin = 'South Korea';

上面的代码中,我们使用了点号表示法dot notation。如果我们现在输出这个对象,便可以看到它具有 origin 属性。接下来,因为这个人物在游戏中有着与众不同的橘色头发,我们可以通过方括号表示法来为它添加这个属性,像这样:

tekkenCharacter['hair color'] = 'dyed orange';

如果要设置的属性中存在空格,或者要设置的属性是一个变量,那我们必须使用方括号表示法bracket notation来为对象添加属性。在上面的代码中,我们把属性 hair color 放到引号里,以此来表示整个字符串都是需要设置的属性。如果我们不加上引号,那么中括号里的内容会被当作一个变量来解析,这个变量对应的值就会作为要设置的属性,请看这段代码:

const eyes = 'eye color';

tekkenCharacter[eyes] = 'brown';

执行以上所有示例代码后,对象会变成这样:

{
  player: 'Hwoarang',
  fightingStyle: 'Tae Kwon Doe',
  human: true,
  origin: 'South Korea',
  'hair color': 'dyed orange',
  'eye color': 'brown'
};

--instructions--

我们已经为你创建了 foods 对象。请使用上述任意语法,来为 foods 对象添加如下三个键值对:bananas 属性,值为 13grapes 属性,值为 35strawberries 属性,值为 27

--hints--

foods 应为一个对象。

assert(typeof foods === 'object');

foods 应有一个值为 13"bananas" 属性。

assert(foods.bananas === 13);

foods 应有一个值为 35"grapes" 属性。

assert(foods.grapes === 35);

foods 应有一个值为 27"strawberries" 属性。

assert(foods.strawberries === 27);

应使用点号表示法或方括号表示法来设置对象的属性。

assert(
  code.search(/bananas:/) === -1 &&
    code.search(/grapes:/) === -1 &&
    code.search(/strawberries:/) === -1
);

--seed--

--seed-contents--

let foods = {
  apples: 25,
  oranges: 32,
  plums: 28
};

// Only change code below this line

// Only change code above this line

console.log(foods);

--solutions--

let foods = {
  apples: 25,
  oranges: 32,
  plums: 28
};

foods['bananas'] = 13;
foods['grapes']  = 35;
foods['strawberries'] = 27;