2.0 KiB
2.0 KiB
id, title, challengeType, videoUrl, localeTitle
id | title | challengeType | videoUrl | localeTitle |
---|---|---|---|---|
587d7b7e367417b2b2512b24 | Use the Conditional (Ternary) Operator | 1 | 使用条件(三元)运算符 |
Description
condition ? statement-if-true : statement-if-false;
以下函数使用if-else语句来检查条件: function findGreater(a,b){这可以使用
if(a> b){
返回“a更大”;
}
其他{
返回“b更大”;
}
}
conditional operator
重写: function findGreater(a,b){
返回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