2.0 KiB
Raw Blame History

id, title, challengeType, videoUrl, localeTitle
id title challengeType videoUrl localeTitle
587d7b7e367417b2b2512b24 Use the Conditional (Ternary) Operator 1 使用条件(三元)运算符

Description

条件运算符 (也称为三元运算符 可以用作一行if-else表达式。语法是 condition ? statement-if-true : statement-if-false;以下函数使用if-else语句来检查条件
function findGreaterab{
ifa> b{
返回“a更大”;
}
其他{
返回“b更大”;
}
}
这可以使用conditional operator重写:
function findGreaterab{
返回a> b “a更大”“b更大”;
}

Instructions

checkEqual函数中使用conditional operator来检查两个数字是否相等。该函数应返回true或false。

Tests

tests:
  - text: <code>checkEqual</code>应该使用<code>conditional operator</code>
    testString: 'assert(/.+?\s*?\?\s*?.+?\s*?:\s*?.+?/gi.test(code), "<code>checkEqual</code> should use the <code>conditional operator</code>");'
  - text: '<code>checkEqual(1, 2)</code>应该返回false'
    testString: 'assert(checkEqual(1, 2) === false, "<code>checkEqual(1, 2)</code> should return false");'
  - text: '<code>checkEqual(1, 1)</code>应该返回true'
    testString: 'assert(checkEqual(1, 1) === true, "<code>checkEqual(1, 1)</code> should return true");'
  - text: '<code>checkEqual(1, -1)</code>应该返回false'
    testString: 'assert(checkEqual(1, -1) === false, "<code>checkEqual(1, -1)</code> should return false");'

Challenge Seed

function checkEqual(a, b) {

}

checkEqual(1, 2);

Solution

// solution required