2018-10-10 18:03:03 -04:00
|
|
|
|
---
|
|
|
|
|
id: 599a789b454f2bbd91a3ff4d
|
|
|
|
|
challengeType: 1
|
2020-04-29 18:29:13 +08:00
|
|
|
|
videoUrl: 'https://scrimba.com/c/cm8PqCa'
|
|
|
|
|
forumTopicId: 301174
|
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'>
|
|
|
|
|
在上两个挑战中,我们学习了相等运算符 (<code>==</code>) 和严格相等运算符 (<code>===</code>)。现在让我们快速回顾并实践一下。
|
|
|
|
|
如果要比较的值不是同一类型,相等运算符会先执行数据类型转换,然后比较值。而严格相等运算符只比较值,不会进行数据类型转换。
|
|
|
|
|
由此可见,相等运算符和严格相等运算符的区别是:前者会执行隐式类型转换,后者不会。
|
|
|
|
|
<strong>示例</strong>
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
3 == '3' // returns true because JavaScript performs type conversion from string to number
|
|
|
|
|
3 === '3' // returns false because the types are different and type conversion is not performed
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
<strong>提示</strong><br>在JavaScript中,你可以使用<code>typeof</code>运算符确定变量的类型或值,如下所示:
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
typeof 3 // returns 'number'
|
|
|
|
|
typeof '3' // returns 'string'
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</section>
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
## Instructions
|
2020-04-29 18:29:13 +08:00
|
|
|
|
<section id='instructions'>
|
|
|
|
|
编辑器中的<code>compareEquality</code>函数使用相等运算符比较两个值。修改函数,使其仅在值严格相等时返回 "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>compareEquality(10, "10")</code>应该返回 "Not Equal"。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(compareEquality(10, "10") === "Not Equal");
|
2020-04-29 18:29:13 +08:00
|
|
|
|
- text: <code>compareEquality("20", 20)</code>应该返回 "Not Equal"。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(compareEquality("20", 20) === "Not Equal");
|
2020-04-29 18:29:13 +08:00
|
|
|
|
- text: 你应该使用<code>===</code>运算符。
|
2020-02-18 01:40:55 +09:00
|
|
|
|
testString: assert(code.match(/===/g));
|
2018-10-10 18:03:03 -04:00
|
|
|
|
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
## Challenge Seed
|
|
|
|
|
<section id='challengeSeed'>
|
|
|
|
|
|
|
|
|
|
<div id='js-seed'>
|
|
|
|
|
|
|
|
|
|
```js
|
|
|
|
|
// Setup
|
|
|
|
|
function compareEquality(a, b) {
|
|
|
|
|
if (a == b) { // Change this line
|
|
|
|
|
return "Equal";
|
|
|
|
|
}
|
|
|
|
|
return "Not Equal";
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Change this value to test
|
|
|
|
|
compareEquality(10, "10");
|
|
|
|
|
```
|
|
|
|
|
|
|
|
|
|
</div>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
</section>
|
|
|
|
|
|
|
|
|
|
## Solution
|
|
|
|
|
<section id='solution'>
|
|
|
|
|
|
2020-04-29 18:29:13 +08:00
|
|
|
|
|
2018-10-10 18:03:03 -04:00
|
|
|
|
```js
|
2020-04-29 18:29:13 +08:00
|
|
|
|
function compareEquality(a,b) {
|
|
|
|
|
if (a === b) {
|
|
|
|
|
return "Equal";
|
|
|
|
|
}
|
|
|
|
|
return "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>
|