You’re building a React app for a zoo website. The app needs to display a welcome message based on the visitor’s age. Here’s the code you’re working with:
import React from 'react'; function ZooGreeter({ visitorAge }) { if (visitorAge < 13) { return <h1>Hey, young explorer! Check out the lions!</h1>; } else { return <h1>Welcome, visitor! Enjoy the exhibits.</h1>; } }
A new requirement comes in: if the visitor is over 65, display "Greetings, wise one! Visit the bird sanctuary." Which of the following changes would correctly implement this?
Replace the else with if (visitorAge > 65)
else
if (visitorAge > 65)
Add visitorAge > 65 && <h1>Greetings, wise one!</h1> inside the else block
visitorAge > 65 && <h1>Greetings, wise one!</h1>
Use a ternary operator like {visitorAge > 65 ? 'Greetings' : 'Welcome'}
{visitorAge > 65 ? 'Greetings' : 'Welcome'}
Rewrite the whole function using a switch statement
switch
Add an else if (visitorAge > 65) clause before the else
else if (visitorAge > 65)
Imagine you’re debugging a React component for a blog site. The component should show an author’s name, but if it’s missing, it should display "Guest Writer" instead. Here’s the code:
import React from 'react'; function BlogPost({ authorName }) { const displayName = authorName ?? 'Guest Writer'; return <h2>By {displayName}</h2>; }
A teammate runs this with authorName = "" (an empty string) and expects "Guest Writer" to appear, but it shows "By ". Why does this happen, and how could you fix it?
authorName = ""
The ?? operator is broken; rewrite with an if...else
??
if...else
The issue is in JSX; wrap displayName in a <span>
displayName
<span>
The ?? operator only checks for null or undefined, not empty strings; use || instead
null
undefined
||
React can’t render empty strings; add a default prop value
You’re working on a React game app that shows a player’s status. Here’s the code snippet:
import React from 'react'; function PlayerStatus({ score, isActive }) { return ( <div> <h1>Score: {score}</h1> {isActive && <p>You’re in the game!</p>} </div> ); }
The designer wants a message like "Game Over" to show when isActive is false. Which options would achieve this without breaking the current "You’re in the game!" behavior?
isActive
false
Replace the line with if (isActive) return <p>You’re in the game!</p>
if (isActive) return <p>You’re in the game!</p>
Change it to {isActive ? <p>You’re in the game!</p> : <p>Game Over</p>}
{isActive ? <p>You’re in the game!</p> : <p>Game Over</p>}
Use {!isActive && <p>Game Over</p>} below the current line
{!isActive && <p>Game Over</p>}
Add || <p>Game Over</p> after the && expression
|| <p>Game Over</p>
&&
Add {isActive ?? <p>Game Over</p>}
{isActive ?? <p>Game Over</p>}
You’re building a profile card component for a social app. The bio should only show if it exists, and the greeting should differ by age. Here’s the code:
import React from 'react'; function ProfileCard({ name, bio, age }) { return ( <div> <h1>{age < 18 ? 'Hi, kiddo!' : 'Hello,'} {name || 'Unknown'}</h1> {bio && <p>Bio: {bio}</p>} </div> ); }
A user submits name = null, bio = "Loves coding", and age = 15. What will render, and is it correct?
name = null
bio = "Loves coding"
age = 15
"Hi, kiddo! Unknown" and "Bio: Loves coding" — Yes, it’s correct
"Hi, kiddo! Unknown" and no bio — No, it’s incorrect
"Hi, kiddo! null" and "Bio: Loves coding" — No, it’s incorrect
"Hello, Unknown" and no bio — No, it’s incorrect
You’re reviewing a teammate’s code for a weather app:
import React from 'react'; function WeatherDisplay({ temp }) { return ( <div> <h1>{temp < 0 ? 'Brr, it’s cold!' : temp > 30 ? 'Hot out there!' : 'Nice day!'}</h1> </div> ); }
The app needs to handle temp = undefined gracefully by showing "No data yet." Which modification would work?
temp = undefined
Add const displayTemp = temp ?? 'No data yet'; and use {displayTemp}
const displayTemp = temp ?? 'No data yet';
{displayTemp}
Use {temp && (temp < 0 ? 'Brr' : temp > 30 ? 'Hot' : 'Nice')}
{temp && (temp < 0 ? 'Brr' : temp > 30 ? 'Hot' : 'Nice')}
Wrap it in {temp ?? <h1>No data yet</h1>}
{temp ?? <h1>No data yet</h1>}
Change it to {temp !== undefined ? (temp < 0 ? 'Brr' : temp > 30 ? 'Hot' : 'Nice') : 'No data yet'}
{temp !== undefined ? (temp < 0 ? 'Brr' : temp > 30 ? 'Hot' : 'Nice') : 'No data yet'}
This content is only available to registered users.Please register at the the home page.