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'
2018-09-30 23:01:58 +01:00
---
## Description
<section id='description'>
In the previous challenge, you used a single <code>conditional operator</code>. 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:
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";
}
}
```
2018-09-30 23:01:58 +01:00
The above function can be re-written using multiple <code>conditional operators</code>:
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";
}
```
However, this should be used with care as using multiple <code>conditional operators</code> without proper indentation may make your code hard to read. For example:
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";
}
```
2018-09-30 23:01:58 +01:00
</section>
## Instructions
<section id='instructions'>
Use multiple <code>conditional operators</code> in the <code>checkSign</code> function to check if a number is positive, negative or zero.
</section>
## Tests
<section id='tests'>
```yml
2018-10-04 14:37:37 +01:00
tests:
- text: <code>checkSign</code> should use multiple <code>conditional operators</code>
2019-07-13 00:07:53 -07:00
testString: assert(/.+?\s*?\?\s*?.+?\s*?:\s*?.+?\s*?\?\s*?.+?\s*?:\s*?.+?/gi.test(code));
2018-10-04 14:37:37 +01:00
- text: <code>checkSign(10)</code> should return "positive". Note that capitalization matters
2019-07-13 00:07:53 -07:00
testString: assert(checkSign(10) === 'positive');
2018-10-04 14:37:37 +01:00
- text: <code>checkSign(-12)</code> should return "negative". Note that capitalization matters
2019-07-13 00:07:53 -07:00
testString: assert(checkSign(-12) === 'negative');
2018-10-04 14:37:37 +01:00
- text: <code>checkSign(0)</code> should return "zero". Note that capitalization matters
2019-07-13 00:07:53 -07:00
testString: assert(checkSign(0) === 'zero');
2018-09-30 23:01:58 +01:00
```
</section>
## Challenge Seed
<section id='challengeSeed'>
<div id='js-seed'>
```js
function checkSign(num) {
2018-10-08 01:01:53 +01:00
2018-09-30 23:01:58 +01:00
}
checkSign(10);
```
</div>
</section>
## Solution
<section id='solution'>
```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
```
2019-07-18 08:24:12 -07:00
2018-09-30 23:01:58 +01:00
</section>