Challenge - 5 Problems
JSX Expression Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate2:00remaining
What is the output of this React component?
Consider the following React component. What will it render inside the <div>?
React
import React from 'react'; function Greeting() { const name = 'Alice'; const age = 25; return <div>Hello, {name}! You are {age} years old.</div>; } export default Greeting;
Attempts:
2 left
💡 Hint
Remember that expressions inside curly braces in JSX are evaluated and replaced with their values.
✗ Incorrect
In JSX, expressions inside curly braces are evaluated. So {name} becomes 'Alice' and {age} becomes 25, producing the full string.
📝 Syntax
intermediate1:30remaining
Which JSX expression is valid to embed a JavaScript expression?
Which of the following JSX snippets correctly embeds a JavaScript expression to show the sum of 2 + 3?
Attempts:
2 left
💡 Hint
JSX expressions inside curly braces are evaluated as JavaScript.
✗ Incorrect
Option D evaluates 2 + 3 to 5 and renders it. Option D renders the string '2 + 3'. Option D renders the string '2 + 3'. Option D concatenates number 2 and string '3' to '23'.
❓ state_output
advanced2:00remaining
What will be displayed after clicking the button once?
Given this React component, what text will appear inside the <div> after clicking the button once?
React
import React, { useState } from 'react'; function Counter() { const [count, setCount] = useState(0); return ( <> <div>Count: {count}</div> <button onClick={() => setCount(count + 1)}>Increment</button> </> ); } export default Counter;
Attempts:
2 left
💡 Hint
Clicking the button calls setCount with count + 1, updating the state.
✗ Incorrect
Initially count is 0. Clicking the button increases count to 1, so the div shows 'Count: 1'.
🔧 Debug
advanced2:00remaining
Why does this JSX code cause an error?
Examine this JSX snippet. Why does it cause a syntax error?
React
const element = <div>{if (true) { 'Yes' } else { 'No' }}</div>;
Attempts:
2 left
💡 Hint
JSX expressions expect a single JavaScript expression, not statements.
✗ Incorrect
The if statement is a JavaScript statement, not an expression. JSX curly braces only accept expressions like ternary operators.
🧠 Conceptual
expert2:30remaining
What is the value of 'result' after rendering this React component?
Consider this React component. What is the value of the variable 'result' after the component renders?
React
import React from 'react'; function Example() { const a = 5; const b = 10; const result = <>{a > b ? 'Greater' : a < b ? 'Smaller' : 'Equal'}</>; return <div>{result}</div>; } export default Example;
Attempts:
2 left
💡 Hint
The expression uses nested ternary operators to compare a and b.
✗ Incorrect
Since 5 is less than 10, the expression evaluates to 'Smaller'.