0
0
Reactframework~20 mins

Ternary operator usage in React - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Ternary Operator Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2: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;
A<div><h1>Please sign up.</h1></div>
B<div><h1>Welcome back!</h1></div>
C<div></div>
DSyntaxError: Unexpected token
Attempts:
2 left
💡 Hint
Check the value of the isLoggedIn prop and how the ternary operator chooses what to render.
state_output
intermediate
2: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;
AThe switch is OFF
BNo text is displayed
CThe switch is ON
DTypeError: setIsOn is not a function
Attempts:
2 left
💡 Hint
The initial state is false. Clicking the button toggles it to true.
📝 Syntax
advanced
2: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.
Areturn <div>{isActive ? <p>Active</p> : <p>Inactive</p>}</div>;
Breturn <div>{if (isActive) {<p>Active</p>} else {<p>Inactive</p>}}</div>;
Creturn <div>{isActive && <p>Active</p> || <p>Inactive</p>}</div>;
Dreturn <div>{isActive ? <p>Active</p> else <p>Inactive</p>}</div>;
Attempts:
2 left
💡 Hint
Remember that JSX expressions must be valid JavaScript expressions. Ternary operators have a specific syntax.
🔧 Debug
advanced
2: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;
ANested ternary operators without parentheses cause ambiguity and error
BThe component is missing a return statement
CThe component uses invalid JSX tags
DThe code runs correctly without errors
Attempts:
2 left
💡 Hint
Check if nested ternary operators are allowed in JSX without extra parentheses.
🧠 Conceptual
expert
2: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?
ATernary operators allow inline conditional rendering as expressions, which if-else statements cannot do inside JSX
BTernary operators execute faster than if-else statements in React components
CIf-else statements cause React to crash, but ternary operators do not
DTernary operators automatically memoize components, improving performance
Attempts:
2 left
💡 Hint
Think about what JSX expects inside curly braces and how expressions differ from statements.