📖 Scenario: You are creating a simple program to check if a person is old enough to vote. Voting age is 18 years or older.
🎯 Goal: Build a program that uses the ternary operator to decide if a person can vote based on their age.
📋 What You'll Learn
Create a variable called age with a specific number value.
Create a variable called votingAge and set it to 18.
Use the ternary operator with age and votingAge to create a variable canVote that holds the string 'Yes' or 'No'.
Print the value of canVote.
💡 Why This Matters
🌍 Real World
Checking age eligibility is common in many apps like voting, driving licenses, or age-restricted content.
💼 Career
Understanding the ternary operator helps write clean and concise code, a useful skill for any developer.
Progress0 / 4 steps
1
Set the person's age
Create a variable called age and set it to 20.
Javascript
Hint
Use const age = 20; to create the variable.
2
Set the voting age limit
Create a variable called votingAge and set it to 18.
Javascript
Hint
Use const votingAge = 18; to set the voting age.
3
Use the ternary operator to check voting eligibility
Create a variable called canVote that uses the ternary operator to check if age is greater than or equal to votingAge. If yes, set canVote to 'Yes', otherwise 'No'.
Javascript
Hint
Use the ternary operator like this: condition ? valueIfTrue : valueIfFalse.
4
Print the voting eligibility
Write a console.log statement to print the value of canVote.
Javascript
Hint
Use console.log(canVote); to show the result.
Practice
(1/5)
1. What does the ternary operator condition ? expr1 : expr2 do in JavaScript?
easy
A. Executes both expr1 and expr2 and returns their sum.
B. Always returns expr2 regardless of the condition.
C. Throws an error if condition is false.
D. Returns expr1 if condition is true, otherwise returns expr2.
Solution
Step 1: Understand the ternary operator structure
The ternary operator has the form condition ? expr1 : expr2. It checks the condition.
Step 2: Determine the returned value based on the condition
If the condition is true, it returns expr1. Otherwise, it returns expr2.
Final Answer:
Returns expr1 if condition is true, else expr2 -> Option D
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.