* 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>
4.0 KiB
id, title, challengeType, forumTopicId, dashedName
id | title | challengeType | forumTopicId | dashedName |
---|---|---|---|---|
a10d2431ad0c6a099a4b8b52 | 检查对象属性 | 5 | 16011 | everything-be-true |
--description--
对于第一个参数(对象数组)中的每个元素,检查第二个参数(字符串)所对应的属性值是否都为 truthy。
换句话说,函数的第一个参数是一个对象数组,第二个参数是一个字符串 pre
,表示对象的属性。如果数组中的每个对象里,pre
对应属性值均为 truthy
,则返回 true
;否则返回 false
。
JavaScript 中,如果一个值在 Boolean 的上下文中(比如 if
语句)的执行结果为 true
,那么我们称这个值是 truthy
的。
别忘了,你可以使用点号表示法(.
)或方括号表示法([]
)来访问对象的属性。
--hints--
truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}, {"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex")
应返回 true。
assert.strictEqual(
truthCheck(
[
{ user: 'Tinky-Winky', sex: 'male' },
{ user: 'Dipsy', sex: 'male' },
{ user: 'Laa-Laa', sex: 'female' },
{ user: 'Po', sex: 'female' }
],
'sex'
),
true
);
truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy"}, {"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex")
应返回 false。
assert.strictEqual(
truthCheck(
[
{ user: 'Tinky-Winky', sex: 'male' },
{ user: 'Dipsy' },
{ user: 'Laa-Laa', sex: 'female' },
{ user: 'Po', sex: 'female' }
],
'sex'
),
false
);
truthCheck([{"user": "Tinky-Winky", "sex": "male", "age": 0}, {"user": "Dipsy", "sex": "male", "age": 3}, {"user": "Laa-Laa", "sex": "female", "age": 5}, {"user": "Po", "sex": "female", "age": 4}], "age")
应返回 false。
assert.strictEqual(
truthCheck(
[
{ user: 'Tinky-Winky', sex: 'male', age: 2 },
{ user: 'Dipsy', sex: 'male', age: 0 },
{ user: 'Laa-Laa', sex: 'female', age: 5 },
{ user: 'Po', sex: 'female', age: 4 }
],
'age'
),
false
);
truthCheck([{"name": "Pete", "onBoat": true}, {"name": "Repeat", "onBoat": true}, {"name": "FastForward", "onBoat": null}], "onBoat")
应返回 false。
assert.strictEqual(
truthCheck(
[
{ name: 'Pete', onBoat: true },
{ name: 'Repeat', onBoat: true },
{ name: 'FastForward', onBoat: null }
],
'onBoat'
),
false
);
truthCheck([{"name": "Pete", "onBoat": true}, {"name": "Repeat", "onBoat": true, "alias": "Repete"}, {"name": "FastForward", "onBoat": true}], "onBoat")
应返回 true。
assert.strictEqual(
truthCheck(
[
{ name: 'Pete', onBoat: true },
{ name: 'Repeat', onBoat: true, alias: 'Repete' },
{ name: 'FastForward', onBoat: true }
],
'onBoat'
),
true
);
truthCheck([{"single": "yes"}], "single")
应返回 true。
assert.strictEqual(truthCheck([{ single: 'yes' }], 'single'), true);
truthCheck([{"single": ""}, {"single": "double"}], "single")
应返回 false。
assert.strictEqual(
truthCheck([{ single: '' }, { single: 'double' }], 'single'),
false
);
truthCheck([{"single": "double"}, {"single": undefined}], "single")
应返回 false。
assert.strictEqual(
truthCheck([{ single: 'double' }, { single: undefined }], 'single'),
false
);
truthCheck([{"single": "double"}, {"single": NaN}], "single")
应返回 false。
assert.strictEqual(
truthCheck([{ single: 'double' }, { single: NaN }], 'single'),
false
);
--seed--
--seed-contents--
function truthCheck(collection, pre) {
return pre;
}
truthCheck([{"user": "Tinky-Winky", "sex": "male"}, {"user": "Dipsy", "sex": "male"}, {"user": "Laa-Laa", "sex": "female"}, {"user": "Po", "sex": "female"}], "sex");
--solutions--
function truthCheck(collection, pre) {
return collection.every(function(e) { return e[pre]; });
}