B. Incorrect use of multiple ternary operators is not allowed.
C. Missing parentheses around nested ternary expressions.
D. The variable grade is not declared properly.
Solution
Step 1: Analyze the nested ternary expression
The expression uses nested ternary operators without parentheses, which is allowed in JavaScript.
Step 2: Evaluate the condition with score = 85
First condition score > 90 is false, so it checks score > 80, which is true, so grade is 'B'.
Final Answer:
The code is correct and will output 'B'. -> Option A
Quick Check:
Nested ternary without parentheses works [OK]
Hint: Nested ternary operators can be used without parentheses [OK]
Common Mistakes:
Assuming nested ternary needs parentheses
Thinking multiple ternaries are invalid syntax
Confusing variable declaration errors
5. You want to assign a variable status based on a number score: if score is 0 or less, status should be 'Fail'; if between 1 and 50, 'Pass'; otherwise 'Excellent'. Which ternary expression correctly does this?
hard
A. const status = score <= 0 ? 'Fail' : (score > 50 ? 'Pass' : 'Excellent');
B. const status = score < 0 ? 'Fail' : score < 50 ? 'Pass' : 'Excellent';
C. const status = score <= 0 ? 'Fail' : score <= 50 ? 'Pass' : 'Excellent';
D. const status = score === 0 ? 'Fail' : score < 50 ? 'Pass' : 'Excellent';
Solution
Step 1: Understand the conditions for status
score <= 0 means 'Fail', between 1 and 50 means 'Pass', and above 50 means 'Excellent'.
Step 2: Check each option against these conditions
const status = score <= 0 ? 'Fail' : score <= 50 ? 'Pass' : 'Excellent'; correctly checks score <= 0 for 'Fail', then score <= 50 for 'Pass', else 'Excellent'. Others have wrong ranges or conditions.
Final Answer:
const status = score <= 0 ? 'Fail' : score <= 50 ? 'Pass' : 'Excellent'; -> Option C
Quick Check:
Correct ranges and order in ternary [OK]
Hint: Check conditions cover all ranges without gaps [OK]
Common Mistakes:
Using wrong comparison operators
Missing equal signs causing wrong ranges
Incorrect order of conditions
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.