Challenge - 5 Problems
Ternary Operator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What will this React component render?
Consider this React functional component using a ternary operator. What will be displayed when is rendered?
React
import React from 'react'; function Greeting({ isLoggedIn }) { return ( <div> {isLoggedIn ? <h1>Welcome back!</h1> : <h1>Please sign up.</h1>} </div> ); } export default Greeting;
Attempts:
2 left
💡 Hint
Check the value of the isLoggedIn prop and how the ternary operator chooses what to render.
✗ Incorrect
The ternary operator checks if isLoggedIn is true. Since it is true, it renders the first expression:
Welcome back!
inside the div.❓ state_output
intermediate2:00remaining
What is the output after clicking the button once?
This React component uses a ternary operator to display text based on state. What text will be shown after the button is clicked once?
React
import React, { useState } from 'react'; function ToggleText() { const [isOn, setIsOn] = useState(false); return ( <div> <p>{isOn ? 'The switch is ON' : 'The switch is OFF'}</p> <button onClick={() => setIsOn(!isOn)}>Toggle</button> </div> ); } export default ToggleText;
Attempts:
2 left
💡 Hint
The initial state is false. Clicking the button toggles it to true.
✗ Incorrect
Initially, isOn is false, so the text shows 'The switch is OFF'. Clicking the button toggles isOn to true, so the text changes to 'The switch is ON'.
📝 Syntax
advanced2:00remaining
Which option correctly uses a ternary operator inside JSX?
Select the option that correctly uses a ternary operator to conditionally render a message inside a React component.
Attempts:
2 left
💡 Hint
Remember that JSX expressions must be valid JavaScript expressions. Ternary operators have a specific syntax.
✗ Incorrect
Option A uses the correct ternary syntax: condition ? exprIfTrue : exprIfFalse. Options B and D have invalid syntax, and A uses logical operators incorrectly.
🔧 Debug
advanced2:00remaining
Why does this React component throw an error?
This component throws an error when rendered. What is the cause?
React
function Status({ status }) { return ( <div> {status === 'loading' ? <p>Loading...</p> : status === 'error' ? <p>Error!</p> : <p>Done</p>} </div> ); } export default Status;
Attempts:
2 left
💡 Hint
Check if nested ternary operators are allowed in JSX without extra parentheses.
✗ Incorrect
Nested ternary operators are valid in JSX and do not cause errors here. The component returns JSX correctly and uses valid tags.
🧠 Conceptual
expert2:00remaining
What is the main advantage of using ternary operators in React JSX?
Why do developers often prefer ternary operators over if-else statements inside JSX?
Attempts:
2 left
💡 Hint
Think about what JSX expects inside curly braces and how expressions differ from statements.
✗ Incorrect
JSX expects expressions inside curly braces. Ternary operators are expressions and can be used inline. If-else statements are statements and cannot be used directly inside JSX.