conditional operator
。您也可以将它们链接在一起以检查多种条件。以下函数使用if,else if和else语句来检查多个条件: function findGreaterOrEqual(a,b){可以使用多个
if(a === b){
返回“a和b相等”;
}
否则如果(a> b){
返回“a更大”;
}
其他{
返回“b更大”;
}
}
conditional operators
重写上述函数: function findGreaterOrEqual(a,b){
返回(a === b)? “a和b相等”:(a> b)? “a更大”:“b更大”;
}
checkSign
函数中使用多个conditional operators
来检查数字是正数,负数还是零。 checkSign
应该使用多个conditional operators
testString: 'assert(/.+?\s*?\?\s*?.+?\s*?:\s*?.+?\s*?\?\s*?.+?\s*?:\s*?.+?/gi.test(code), "checkSign
should use multiple conditional operators
");'
- text: checkSign(10)
应该返回“positive”。请注意,资本化很重要
testString: 'assert(checkSign(10) === "positive", "checkSign(10)
should return "positive". Note that capitalization matters");'
- text: checkSign(-12)
应返回“否定”。请注意,资本化很重要
testString: 'assert(checkSign(-12) === "negative", "checkSign(-12)
should return "negative". Note that capitalization matters");'
- text: checkSign(0)
应返回“零”。请注意,资本化很重要
testString: 'assert(checkSign(0) === "zero", "checkSign(0)
should return "zero". Note that capitalization matters");'
```