2018-10-16 07:57:24 +04:00
|
|
|
|
---
|
|
|
|
|
title: Use Multiple Conditional (Ternary) Operators
|
2018-10-04 14:47:55 +01:00
|
|
|
|
---
|
2018-10-16 07:57:24 +04:00
|
|
|
|
|
2018-10-04 14:47:55 +01:00
|
|
|
|
## Use Multiple Conditional (Ternary) Operators
|
|
|
|
|
|
2018-10-16 07:57:24 +04:00
|
|
|
|
We need to use multiple ```conditional operators``` in the ```checkSign``` function to check if a number is positive, negative or zero.
|
|
|
|
|
|
|
|
|
|
Here’s a solution:
|
|
|
|
|
|
|
|
|
|
In the function body we need to add multiple ```conditional operators``` - as in our lesson:
|
|
|
|
|
|
|
|
|
|
```javascript
|
|
|
|
|
{return (num === 10) ? "positive" : (num === -12) ? "negative" : "zero";}
|
|
|
|
|
```
|
|
|
|
|
In this way, function can check if a number is positive, negative or zero.
|
2018-10-04 14:47:55 +01:00
|
|
|
|
|
2018-10-16 07:57:24 +04:00
|
|
|
|
Here’s a full solution:
|
2018-10-04 14:47:55 +01:00
|
|
|
|
|
2018-10-16 07:57:24 +04:00
|
|
|
|
```javascript
|
|
|
|
|
function checkSign(num) {
|
|
|
|
|
return (num === 10) ? "positive" : (num === -12) ? "negative" : "zero";
|
|
|
|
|
}
|
|
|
|
|
checkSign(10);
|
|
|
|
|
```
|