2.6 KiB
2.6 KiB
id, title, challengeType, videoUrl, localeTitle
id | title | challengeType | videoUrl | localeTitle |
---|---|---|---|---|
56533eb9ac21ba0edf2244d0 | Comparison with the Equality Operator | 1 | 与平等算子的比较 |
Description
true
或false
值。最基本的运算符是等于运算符==
。等于运算符比较两个值,如果它们是等价的则返回true
否则返回false
。请注意,相等性与赋值( =
)不同,后者将运算符右侧的值分配给左侧的变量。 function equalityTest(myVal){如果
if(myVal == 10){
返回“平等”;
}
返回“不等于”;
}
myVal
等于10
,则等于运算符返回true
,因此大括号中的代码将执行,函数将返回"Equal"
。否则,该函数将返回"Not Equal"
。为了使JavaScript能够比较两种不同的data types
(例如, numbers
和strings
),它必须将一种类型转换为另一种类型。这被称为“类型强制”。但是,一旦它完成,它可以比较如下术语: 1 == 1 //是的
1 == 2 //假
1 =='1'//是的
“3”== 3 //是的
Instructions
equality operator
添加到指示的行,以便当val
等于12
时,函数将返回“Equal” Tests
tests:
- text: <code>testEqual(10)</code>应该返回“Not Equal”
testString: 'assert(testEqual(10) === "Not Equal", "<code>testEqual(10)</code> should return "Not Equal"");'
- text: <code>testEqual(12)</code>应返回“Equal”
testString: 'assert(testEqual(12) === "Equal", "<code>testEqual(12)</code> should return "Equal"");'
- text: <code>testEqual("12")</code>应返回“Equal”
testString: 'assert(testEqual("12") === "Equal", "<code>testEqual("12")</code> should return "Equal"");'
- text: 您应该使用<code>==</code>运算符
testString: 'assert(code.match(/==/g) && !code.match(/===/g), "You should use the <code>==</code> operator");'
Challenge Seed
// Setup
function testEqual(val) {
if (val) { // Change this line
return "Equal";
}
return "Not Equal";
}
// Change this value to test
testEqual(10);
Solution
// solution required