64 lines
2.4 KiB
Markdown
64 lines
2.4 KiB
Markdown
|
---
|
||
|
id: 587d7b7e367417b2b2512b21
|
||
|
title: Use Multiple Conditional (Ternary) Operators
|
||
|
challengeType: 1
|
||
|
---
|
||
|
|
||
|
## 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:
|
||
|
<blockquote>function findGreaterOrEqual(a, b) {<br> if(a === b) {<br> return "a and b are equal";<br> }<br> else if(a > b) {<br> return "a is greater";<br> }<br> else {<br> return "b is greater";<br> }<br>}</blockquote>
|
||
|
The above function can be re-written using multiple <code>conditional operators</code>:
|
||
|
<blockquote>function findGreaterOrEqual(a, b) {<br> return (a === b) ? "a and b are equal" : (a > b) ? "a is greater" : "b is greater";<br>}</blockquote>
|
||
|
</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
|
||
|
- text: <code>checkSign</code> should use multiple <code>conditional operators</code>
|
||
|
testString: 'assert(/.+?\s*?\?\s*?.+?\s*?:\s*?.+?\s*?\?\s*?.+?\s*?:\s*?.+?/gi.test(code), "<code>checkSign</code> should use multiple <code>conditional operators</code>");'
|
||
|
- text: <code>checkSign(10)</code> should return "positive". Note that capitalization matters
|
||
|
testString: 'assert(checkSign(10) === "positive", "<code>checkSign(10)</code> should return "positive". Note that capitalization matters");'
|
||
|
- text: <code>checkSign(-12)</code> should return "negative". Note that capitalization matters
|
||
|
testString: 'assert(checkSign(-12) === "negative", "<code>checkSign(-12)</code> should return "negative". Note that capitalization matters");'
|
||
|
- text: <code>checkSign(0)</code> should return "zero". Note that capitalization matters
|
||
|
testString: 'assert(checkSign(0) === "zero", "<code>checkSign(0)</code> should return "zero". Note that capitalization matters");'
|
||
|
|
||
|
```
|
||
|
|
||
|
</section>
|
||
|
|
||
|
## Challenge Seed
|
||
|
<section id='challengeSeed'>
|
||
|
|
||
|
<div id='js-seed'>
|
||
|
|
||
|
```js
|
||
|
function checkSign(num) {
|
||
|
|
||
|
}
|
||
|
|
||
|
checkSign(10);
|
||
|
```
|
||
|
|
||
|
</div>
|
||
|
|
||
|
|
||
|
|
||
|
</section>
|
||
|
|
||
|
## Solution
|
||
|
<section id='solution'>
|
||
|
|
||
|
```js
|
||
|
// solution required
|
||
|
```
|
||
|
</section>
|