2.3 KiB
Raw Blame History

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, &quot;10&quot;)</code>应返回“Not Equal”'
    testString: 'assert(compareEquality(10, "10") === "Not Equal", "<code>compareEquality(10, "10")</code> should return "Not Equal"");'
  - text: '<code>compareEquality(&quot;20&quot;, 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