2.5 KiB
2.5 KiB
id, title, challengeType, videoUrl, forumTopicId, localeTitle
id | title | challengeType | videoUrl | forumTopicId | localeTitle |
---|---|---|---|---|---|
56533eb9ac21ba0edf2244d0 | Comparison with the Equality Operator | 1 | https://scrimba.com/c/cKyVMAL | 16784 | 相等运算符 |
Description
true
或false
值。
最基本的运算符是相等运算符:==
。相等运算符比较两个值,如果它们是同等,返回true
,如果它们不等,返回false
。值得注意的是相等运算符不同于赋值运算符(=
),赋值运算符是把等号右边的值赋给左边的变量。
function equalityTest(myVal) {
if (myVal == 10) {
return "Equal";
}
return "Not Equal";
}
如果myVal
等于10
,相等运算符会返回true
,因此大括号里面的代码会被执行,函数将返回"Equal"
。否则,函数返回"Not Equal"
。
在 JavaScript 中,为了让两个不同的数据类型
(例如数字
和字符串
)的值可以作比较,它必须把一种类型转换为另一种类型。然而一旦这样做,它可以像下面这样来比较:
1 == 1 // true
1 == 2 // false
1 == '1' // true
"3" == 3 // true
Instructions
相等运算符
添加到指定的行,这样当val
的值为12
的时候,函数会返回"Equal"。
Tests
tests:
- text: <code>testEqual(10)</code>应该返回 "Not Equal"。
testString: assert(testEqual(10) === "Not Equal");
- text: <code>testEqual(12)</code>应该返回 "Equal"。
testString: assert(testEqual(12) === "Equal");
- text: <code>testEqual("12")</code>应该返回 "Equal"。
testString: assert(testEqual("12") === "Equal");
- text: 你应该使用<code>==</code>运算符。
testString: assert(code.match(/==/g) && !code.match(/===/g));
Challenge Seed
// Setup
function testEqual(val) {
if (val) { // Change this line
return "Equal";
}
return "Not Equal";
}
// Change this value to test
testEqual(10);
Solution
function testEqual(val) {
if (val == 12) {
return "Equal";
}
return "Not Equal";
}