2018-09-30 23:01:58 +01:00
---
id: 587d7b7e367417b2b2512b21
title: Use Multiple Conditional (Ternary) Operators
challengeType: 1
2019-02-14 12:24:02 -05:00
videoUrl: 'https://scrimba.com/c/cyWJBT4'
2019-08-05 09:17:33 -07:00
forumTopicId: 301179
2018-09-30 23:01:58 +01:00
---
2020-11-27 19:02:05 +01:00
# --description--
2019-10-27 15:45:37 -01: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 19:02:05 +01:00
2018-09-30 23:01:58 +01:00
The following function uses if, else if, and else statements to check multiple conditions:
2019-05-17 06:20:30 -07: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 15:45:37 -01:00
The above function can be re-written using multiple conditional operators:
2019-05-17 06:20:30 -07:00
2019-06-15 20:06:56 +02: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 09:33:03 -07: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 20:06:56 +02:00
2019-05-17 06:20:30 -07: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 19:02:05 +01:00
# --instructions--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01: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 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --hints--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
`checkSign` should use multiple conditional operators
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(/.+?\s*?\?\s*?.+?\s*?:\s*?.+?\s*?\?\s*?.+?\s*?:\s*?.+?/gi.test(code));
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
`checkSign(10)` should return "positive". Note that capitalization matters
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(checkSign(10) === 'positive');
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
`checkSign(-12)` should return "negative". Note that capitalization matters
2018-09-30 23:01:58 +01:00
```js
2020-11-27 19:02:05 +01:00
assert(checkSign(-12) === 'negative');
```
2018-10-08 01:01:53 +01:00
2020-11-27 19:02:05 +01:00
`checkSign(0)` should return "zero". Note that capitalization matters
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
assert(checkSign(0) === 'zero');
2018-09-30 23:01:58 +01:00
```
2020-11-27 19:02:05 +01:00
# --seed--
## --seed-contents--
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
```js
function checkSign(num) {
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
}
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
checkSign(10);
```
2018-09-30 23:01:58 +01:00
2020-11-27 19:02:05 +01:00
# --solutions--
2018-09-30 23:01:58 +01:00
```js
2018-10-16 05:16:43 +05:30
function checkSign(num) {
return (num > 0) ? 'positive' : (num < 0 ) ? ' negative ' : ' zero ' ;
}
2018-09-30 23:01:58 +01:00
```