freeCodeCamp/curriculum/challenges/chinese/02-javascript-algorithms-an.../basic-javascript/comparison-with-the-equalit...

2.6 KiB
Raw Blame History

id title challengeType videoUrl localeTitle
56533eb9ac21ba0edf2244d0 Comparison with the Equality Operator 1 与平等算子的比较

Description

有很多比较运算符在JavaScript中。所有这些运算符都返回布尔值truefalse值。最基本的运算符是等于运算符== 。等于运算符比较两个值,如果它们是等价的则返回true否则返回false 。请注意,相等性与赋值( = )不同,后者将运算符右侧的值分配给左侧的变量。
function equalityTestmyVal{
ifmyVal == 10{
返回“平等”;
}
返回“不等于”;
}
如果myVal等于10 ,则等于运算符返回true ,因此大括号中的代码将执行,函数将返回"Equal" 。否则,该函数将返回"Not Equal" 。为了使JavaScript能够比较两种不同的data types (例如, numbersstrings ),它必须将一种类型转换为另一种类型。这被称为“类型强制”。但是,一旦它完成,它可以比较如下术语:
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(&quot;12&quot;)</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