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