2018-10-10 18:03:03 -04:00
---
id: 587d7b7e367417b2b2512b21
2021-02-06 04:42:36 +00:00
title: Use Multiple Conditional (Ternary) Operators
2018-10-10 18:03:03 -04:00
challengeType: 1
2020-04-29 18:29:13 +08:00
videoUrl: 'https://scrimba.com/c/cyWJBT4'
forumTopicId: 301179
2021-01-13 03:31:00 +01:00
dashedName: use-multiple-conditional-ternary-operators
2018-10-10 18:03:03 -04:00
---
2020-12-16 00:37:30 -07:00
# --description--
2021-02-06 04:42:36 +00:00
In the previous challenge, you used a single conditional operator. You can also chain them together to check for multiple conditions.
2020-12-16 00:37:30 -07:00
2021-02-06 04:42:36 +00:00
The following function uses if, else if, and else statements to check multiple conditions:
2020-04-29 18:29:13 +08:00
```js
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";
}
}
```
2021-02-06 04:42:36 +00:00
The above function can be re-written using multiple conditional operators:
2020-04-29 18:29:13 +08:00
```js
function findGreaterOrEqual(a, b) {
return (a === b) ? "a and b are equal"
: (a > b) ? "a is greater"
: "b is greater";
}
```
2021-02-06 04:42:36 +00:00
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:
2020-04-29 18:29:13 +08:00
```js
function findGreaterOrEqual(a, b) {
return (a === b) ? "a and b are equal" : (a > b) ? "a is greater" : "b is greater";
}
```
2020-12-16 00:37:30 -07:00
# --instructions--
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
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"` .
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
# --hints--
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
`checkSign` should use multiple conditional operators
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert(/.+?\s*?\?\s*?.+?\s*?:\s*?.+?\s*?\?\s*?.+?\s*?:\s*?.+?/gi.test(code));
2018-10-10 18:03:03 -04:00
```
2021-02-06 04:42:36 +00:00
`checkSign(10)` should return "positive". Note that capitalization matters
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(checkSign(10) === 'positive');
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
`checkSign(-12)` should return "negative". Note that capitalization matters
2018-10-10 18:03:03 -04:00
2020-12-16 00:37:30 -07:00
```js
assert(checkSign(-12) === 'negative');
```
2018-10-10 18:03:03 -04:00
2021-02-06 04:42:36 +00:00
`checkSign(0)` should return "zero". Note that capitalization matters
2018-10-10 18:03:03 -04:00
```js
2020-12-16 00:37:30 -07:00
assert(checkSign(0) === 'zero');
2018-10-10 18:03:03 -04:00
```
2020-04-29 18:29:13 +08:00
2021-01-13 03:31:00 +01:00
# --seed--
## --seed-contents--
```js
function checkSign(num) {
}
checkSign(10);
```
2020-12-16 00:37:30 -07:00
# --solutions--
2021-01-13 03:31:00 +01:00
```js
function checkSign(num) {
return (num > 0) ? 'positive' : (num < 0 ) ? ' negative ' : ' zero ' ;
}
```