Pie Logo
Quiz: Conditional Rendering
by Tomas Trescak

QuizTest your understanding of the section content.

ContentDetailed description of the section content.

Full Stack Development
Introduction
Generative AI
🍰 Player
💼 Editor
Setup
React Fundamentals
JSX and Components
Props
Assignment 1.2: Front End
CSS Styles
useState and Hooks
Conditional Rendering
Quiz: Conditional Rendering
Lists
Forms and Events
Storybook
Assignment 1.3: Bonus - Backend
Leaderboards
Next.js
Advanced React
Databases
React Hooks
Authentication and Authorisation
APIs
CI/CD and DevOps
Testing React
Advanced Topics
    Pie Logo
    Quiz: Conditional Rendering
    by Tomas Trescak

    QuizTest your understanding of the section content.

    ContentDetailed description of the section content.

    Full Stack Development
    Introduction
    Generative AI
    🍰 Player
    💼 Editor
    Setup
    React Fundamentals
    JSX and Components
    Props
    Assignment 1.2: Front End
    CSS Styles
    useState and Hooks
    Conditional Rendering
    Quiz: Conditional Rendering
    Lists
    Forms and Events
    Storybook
    Assignment 1.3: Bonus - Backend
    Leaderboards
    Next.js
    Advanced React
    Databases
    React Hooks
    Authentication and Authorisation
    APIs
    CI/CD and DevOps
    Testing React
    Advanced Topics

    Conditional Rendering
    by Tomas Trescak· Conditional Rendering

    QUESTION 1

    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?


    QUESTION 2

    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?


    QUESTION 3

    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?


    QUESTION 4

    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?


    QUESTION 5

    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?

    This content is only available to registered users.
    Please register at the the home page.

    Conditional Rendering
    by Tomas Trescak· Conditional Rendering

    QUESTION 1

    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?


    QUESTION 2

    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?


    QUESTION 3

    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?


    QUESTION 4

    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?


    QUESTION 5

    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?

    This content is only available to registered users.
    Please register at the the home page.

    Pie Logo
    Quiz: Conditional Rendering
    by Tomas Trescak
    Pie Logo
    Quiz: Conditional Rendering
    by Tomas Trescak
    Full Stack Development
    Introduction
    Generative AI
    🍰 Player
    💼 Editor
    Setup
    React Fundamentals
    JSX and Components
    Props
    Assignment 1.2: Front End
    CSS Styles
    useState and Hooks
    Conditional Rendering
    Quiz: Conditional Rendering
    Lists
    Forms and Events
    Storybook
    Assignment 1.3: Bonus - Backend
    Leaderboards
    Next.js
    Advanced React
    Databases
    React Hooks
    Authentication and Authorisation
    APIs
    CI/CD and DevOps
    Testing React
    Advanced Topics
    Full Stack Development
    Introduction
    Generative AI
    🍰 Player
    💼 Editor
    Setup
    React Fundamentals
    JSX and Components
    Props
    Assignment 1.2: Front End
    CSS Styles
    useState and Hooks
    Conditional Rendering
    Quiz: Conditional Rendering
    Lists
    Forms and Events
    Storybook
    Assignment 1.3: Bonus - Backend
    Leaderboards
    Next.js
    Advanced React
    Databases
    React Hooks
    Authentication and Authorisation
    APIs
    CI/CD and DevOps
    Testing React
    Advanced Topics