* 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.2 KiB
id, title, challengeType, videoUrl, forumTopicId, dashedName
id | title | challengeType | videoUrl | forumTopicId | dashedName |
---|---|---|---|---|---|
587d7b7e367417b2b2512b21 | Use Multiple Conditional (Ternary) Operators | 1 | https://scrimba.com/c/cyWJBT4 | 301179 | use-multiple-conditional-ternary-operators |
--description--
In the previous challenge, you used a single conditional operator. You can also chain them together to check for multiple conditions.
The following function uses if, else if, and else statements to check multiple conditions:
function findGreaterOrEqual(a, b) {
if (a === b) {
return "a and b are equal";
}
else if (a > b) {
return "a is greater";
}
else {
return "b is greater";
}
}
The above function can be re-written using multiple conditional operators:
function findGreaterOrEqual(a, b) {
return (a === b) ? "a and b are equal"
: (a > b) ? "a is greater"
: "b is greater";
}
It is considered best practice to format multiple conditional operators such that each condition is on a separate line, as shown above. Using multiple conditional operators without proper indentation may make your code hard to read. For example:
function findGreaterOrEqual(a, b) {
return (a === b) ? "a and b are equal" : (a > b) ? "a is greater" : "b is greater";
}
--instructions--
In the checkSign
function, use multiple conditional operators - following the recommended format used in findGreaterOrEqual
- to check if a number is positive, negative or zero. The function should return "positive"
, "negative"
or "zero"
.
--hints--
checkSign
should use multiple conditional operators
assert(/.+?\s*?\?\s*?.+?\s*?:\s*?.+?\s*?\?\s*?.+?\s*?:\s*?.+?/gi.test(code));
checkSign(10)
should return "positive". Note that capitalization matters
assert(checkSign(10) === 'positive');
checkSign(-12)
should return "negative". Note that capitalization matters
assert(checkSign(-12) === 'negative');
checkSign(0)
should return "zero". Note that capitalization matters
assert(checkSign(0) === 'zero');
--seed--
--seed-contents--
function checkSign(num) {
}
checkSign(10);
--solutions--
function checkSign(num) {
return (num > 0) ? 'positive' : (num < 0) ? 'negative' : 'zero';
}