* Use dfn tags * remove misused <dfn> tags * Revert "remove misused <dfn> tags" This reverts commit b24968a96810f618d831410ac90a0bc452ebde50. * Update curriculum/challenges/english/01-responsive-web-design/basic-html-and-html5/fill-in-the-blank-with-placeholder-text.english.md Co-Authored-By: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> * Make "array" lowercase Co-Authored-By: Randell Dawson <5313213+RandellDawson@users.noreply.github.com> * Fix dfn usage * Address last dfn tags
2.3 KiB
2.3 KiB
id, title, challengeType, videoUrl, forumTopicId
id | title | challengeType | videoUrl | forumTopicId |
---|---|---|---|---|
587d7b7e367417b2b2512b21 | Use Multiple Conditional (Ternary) Operators | 1 | https://scrimba.com/c/cyWJBT4 | 301179 |
Description
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";
}
However, this should be used with care as 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
checkSign
function to check if a number is positive, negative or zero. The function should return "positive", "negative" or "zero".
Tests
tests:
- text: <code>checkSign</code> should use multiple conditional operators
testString: assert(/.+?\s*?\?\s*?.+?\s*?:\s*?.+?\s*?\?\s*?.+?\s*?:\s*?.+?/gi.test(code));
- text: <code>checkSign(10)</code> should return "positive". Note that capitalization matters
testString: assert(checkSign(10) === 'positive');
- text: <code>checkSign(-12)</code> should return "negative". Note that capitalization matters
testString: assert(checkSign(-12) === 'negative');
- text: <code>checkSign(0)</code> should return "zero". Note that capitalization matters
testString: assert(checkSign(0) === 'zero');
Challenge Seed
function checkSign(num) {
}
checkSign(10);
Solution
function checkSign(num) {
return (num > 0) ? 'positive' : (num < 0) ? 'negative' : 'zero';
}