Challenge - 5 Problems
JSX Condition Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What does this React component render?
Consider this React component using an if condition inside JSX. What will it display when
isLoggedIn is true?React
function Greeting({ isLoggedIn }) { return ( <div> {isLoggedIn ? <h1>Welcome back!</h1> : <h1>Please sign up.</h1>} </div> ); }
Attempts:
2 left
💡 Hint
Look at the ternary operator inside the JSX and what happens when the condition is true.
✗ Incorrect
The ternary operator checks if
isLoggedIn is true. If yes, it renders <h1>Welcome back!</h1>. Otherwise, it renders <h1>Please sign up.</h1>.📝 Syntax
intermediate2:00remaining
Which option correctly uses an if statement inside JSX?
You want to conditionally render a message only if
showMessage is true. Which code snippet is valid React JSX?Attempts:
2 left
💡 Hint
Remember that JSX expressions cannot contain statements like if directly.
✗ Incorrect
JSX allows expressions but not statements inside curly braces. Using
showMessage && <p>Hello!</p> is a common pattern to conditionally render elements.🔧 Debug
advanced2:00remaining
Why does this React component cause an error?
Look at this component code. Why does it cause a syntax error?
React
function Status({ isOnline }) { return ( <div> {if (isOnline) { <span>Online</span> } else { <span>Offline</span> }} </div> ); }
Attempts:
2 left
💡 Hint
Think about what JSX allows inside curly braces.
✗ Incorrect
JSX curly braces expect expressions, not statements like if. Using if inside curly braces causes a syntax error.
❓ state_output
advanced2:00remaining
What is the output after clicking the button twice?
This React component toggles a message when the button is clicked. What will it display after two clicks?
React
import React, { useState } from 'react'; function ToggleMessage() { const [show, setShow] = useState(false); return ( <div> <button onClick={() => setShow(!show)}>Toggle</button> {show && <p>The message is visible</p>} </div> ); }
Attempts:
2 left
💡 Hint
Each click toggles the boolean state. Think about the initial state and toggling twice.
✗ Incorrect
Initial state is false, so no message. First click sets show to true, message appears. Second click sets show back to false, message disappears.
🧠 Conceptual
expert3:00remaining
Which approach correctly handles multiple if conditions in JSX?
You want to render different greetings based on the time of day using JSX. Which option correctly implements multiple conditions?
Attempts:
2 left
💡 Hint
JSX allows nested ternary operators but not if statements inside curly braces.
✗ Incorrect
Option A uses nested ternary operators, which is valid JSX. Option A and D use if statements inside JSX, which is invalid. Option A renders multiple paragraphs if conditions overlap.