* 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>
2.4 KiB
2.4 KiB
id, title, challengeType, forumTopicId, dashedName
id | title | challengeType | forumTopicId | dashedName |
---|---|---|---|---|
587d7b7e367417b2b2512b20 | 使用数组存储不同类型的数据 | 1 | 301167 | use-an-array-to-store-a-collection-of-data |
--description--
以下是最简单的数组(Array)示例:这是一个一维数组(one-dimensional array),它只有一层,或者说它里面没有包含其它数组。可以观察到,这个数组中只包含了布尔值(booleans)、字符串(strings)、数字(numbers)以及 JavaScript 中的其他数据类型:
let simpleArray = ['one', 2, 'three', true, false, undefined, null];
console.log(simpleArray.length);
// 输出 7
所有数组都有一个表示长度的属性,我们可以通过 Array.length
来访问它。接下来是一个稍复杂的数组示例:这是一个多维数组(multi-dimensional Array),或者说是一个包含了其他数组的数组。可以观察到,在这个数组内部还包含了 JavaScript 的对象(objects),我们会在后面的挑战中详细讨论该数据结构。现在,你只需要知道数组能够存储复杂的对象数据。
let complexArray = [
[
{
one: 1,
two: 2
},
{
three: 3,
four: 4
}
],
[
{
a: "a",
b: "b"
},
{
c: "c",
d: "d"
}
]
];
--instructions--
我们已经定义了一个名为 yourArray
的变量。请修改代码,将一个含有至少 5 个元素的数组赋值给 yourArray
变量。你的数组中应包含至少一个 string 类型的数据、一个 number 类型的数据和一个 boolean 类型的数据。
--hints--
yourArray 应为数组。
assert.strictEqual(Array.isArray(yourArray), true);
yourArray
应包含至少 5 个元素。
assert.isAtLeast(yourArray.length, 5);
yourArray
应包含至少一个 boolean
。
assert(yourArray.filter((el) => typeof el === 'boolean').length >= 1);
yourArray
应包含至少一个 number
。
assert(yourArray.filter((el) => typeof el === 'number').length >= 1);
yourArray
应包含至少一个 string
。
assert(yourArray.filter((el) => typeof el === 'string').length >= 1);
--seed--
--seed-contents--
let yourArray; // Change this line
--solutions--
let yourArray = ['a string', 100, true, ['one', 2], 'another string'];