2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
id: 587d7b7e367417b2b2512b24
|
|
|
|
challengeType: 1
|
2020-04-29 18:29:13 +08:00
|
|
|
videoUrl: 'https://scrimba.com/c/c3JRmSg'
|
|
|
|
forumTopicId: 301181
|
2020-10-01 17:54:21 +02:00
|
|
|
title: 使用三元运算符
|
2018-10-10 18:03:03 -04:00
|
|
|
---
|
|
|
|
|
|
|
|
## Description
|
2020-04-29 18:29:13 +08:00
|
|
|
<section id='description'>
|
|
|
|
条件运算符(也称为三元运算符)的用处就像写成一行的 if-else 表达式
|
|
|
|
语法如下所示:
|
|
|
|
<code>condition ? statement-if-true : statement-if-false;</code>
|
|
|
|
以下函数使用 if-else 语句来检查条件:
|
|
|
|
|
|
|
|
```js
|
|
|
|
function findGreater(a, b) {
|
|
|
|
if(a > b) {
|
|
|
|
return "a is greater";
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return "b is greater";
|
|
|
|
}
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
上面的函数使用条件运算符写法如下:
|
|
|
|
|
|
|
|
```js
|
|
|
|
function findGreater(a, b) {
|
|
|
|
return a > b ? "a is greater" : "b is greater";
|
|
|
|
}
|
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
## Instructions
|
2020-04-29 18:29:13 +08:00
|
|
|
<section id='instructions'>
|
|
|
|
在<code>checkEqual</code>函数中使用条件运算符检查两个数字是否相等,函数应该返回 "Equal" 或 "Not Equal"
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
## Tests
|
|
|
|
<section id='tests'>
|
|
|
|
|
|
|
|
```yml
|
|
|
|
tests:
|
2020-04-29 18:29:13 +08:00
|
|
|
- text: <code>checkEqual</code>应该使用条件运算符。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(/.+?\s*?\?\s*?.+?\s*?:\s*?.+?/.test(code));
|
2020-04-29 18:29:13 +08:00
|
|
|
- text: <code>checkEqual(1, 2)</code>应该返回 "Not Equal"。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(checkEqual(1, 2) === "Not Equal");
|
2020-04-29 18:29:13 +08:00
|
|
|
- text: <code>checkEqual(1, 1)</code>应该返回 "Equal"。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(checkEqual(1, 1) === "Equal");
|
2020-04-29 18:29:13 +08:00
|
|
|
- text: <code>checkEqual(1, -1)</code>应该返回 "Not Equal"。
|
2020-02-18 01:40:55 +09:00
|
|
|
testString: assert(checkEqual(1, -1) === "Not Equal");
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
```js
|
|
|
|
function checkEqual(a, b) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
checkEqual(1, 2);
|
|
|
|
```
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
<section id='solution'>
|
|
|
|
|
|
|
|
```js
|
2020-04-29 18:29:13 +08:00
|
|
|
function checkEqual(a, b) {
|
|
|
|
return a === b ? "Equal" : "Not Equal";
|
|
|
|
}
|
2018-10-10 18:03:03 -04:00
|
|
|
```
|
2020-04-29 18:29:13 +08:00
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
</section>
|