2.3 KiB
2.3 KiB
id, title, challengeType, videoUrl, localeTitle
id | title | challengeType | videoUrl | localeTitle |
---|---|---|---|---|
599a789b454f2bbd91a3ff4d | Practice comparing different values | 1 | 练习比较不同的值 |
Description
==
)和严格相等运算符( ===
)。让我们快速回顾一下这些运算符的实践。如果要比较的值不是同一类型,则相等运算符将执行类型转换,然后计算值。但是,严格相等运算符将按原样比较数据类型和值,而不将一种类型转换为另一种类型。 例子 3 =='3'//返回true,因为JavaScript执行从字符串到数字的类型转换注意
3 ==='3'//返回false,因为类型不同并且未执行类型转换
在JavaScript中,您可以使用
typeof
运算符确定变量的类型或值,如下所示: typeof 3 //返回'number'
typeof'3'//返回'string'
Instructions
compareEquality
函数使用equality operator
比较两个值。修改函数,使其仅在值严格相等时返回“Equal”。 Tests
tests:
- text: '<code>compareEquality(10, "10")</code>应返回“Not Equal”'
testString: 'assert(compareEquality(10, "10") === "Not Equal", "<code>compareEquality(10, "10")</code> should return "Not Equal"");'
- text: '<code>compareEquality("20", 20)</code>应该返回“Not Equal”'
testString: 'assert(compareEquality("20", 20) === "Not Equal", "<code>compareEquality("20", 20)</code> should return "Not Equal"");'
- text: 您应该使用<code>===</code>运算符
testString: 'assert(code.match(/===/g), "You should use the <code>===</code> operator");'
Challenge Seed
// Setup
function compareEquality(a, b) {
if (a == b) { // Change this line
return "Equal";
}
return "Not Equal";
}
// Change this value to test
compareEquality(10, "10");
Solution
// solution required