conditional operator
. You can also chain them together to check for multiple conditions.
The following function uses if, else if, and else statements to check multiple conditions:
function findGreaterOrEqual(a, b) {The above function can be re-written using multiple
if(a === b) {
return "a and b are equal";
}
else if(a > b) {
return "a is greater";
}
else {
return "b is greater";
}
}
conditional operators
:
function findGreaterOrEqual(a, b) {
return (a === b) ? "a and b are equal" : (a > b) ? "a is greater" : "b is greater";
}
conditional operators
in the checkSign
function to check if a number is positive, negative or zero.
checkSign
should use multiple conditional operators
testString: 'assert(/.+?\s*?\?\s*?.+?\s*?:\s*?.+?\s*?\?\s*?.+?\s*?:\s*?.+?/gi.test(code), "checkSign
should use multiple conditional operators
");'
- text: checkSign(10)
should return "positive". Note that capitalization matters
testString: 'assert(checkSign(10) === "positive", "checkSign(10)
should return "positive". Note that capitalization matters");'
- text: checkSign(-12)
should return "negative". Note that capitalization matters
testString: 'assert(checkSign(-12) === "negative", "checkSign(-12)
should return "negative". Note that capitalization matters");'
- text: checkSign(0)
should return "zero". Note that capitalization matters
testString: 'assert(checkSign(0) === "zero", "checkSign(0)
should return "zero". Note that capitalization matters");'
```