0
0
Reactframework~20 mins

Map function usage in React - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Map Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
component_behavior
intermediate
2:00remaining
What is the rendered output of this React component?

Consider this React component that uses map to render a list of items:

function FruitList() {
  const fruits = ['Apple', 'Banana', 'Cherry'];
  return (
    <ul>
      {fruits.map((fruit, index) => <li key={index}>{fruit}</li>)}
    </ul>
  );
}

What will this component render in the browser?

React
function FruitList() {
  const fruits = ['Apple', 'Banana', 'Cherry'];
  return (
    <ul>
      {fruits.map((fruit, index) => <li key={index}>{fruit}</li>)}
    </ul>
  );
}
A<ul>AppleBananaCherry</ul>
B<ul><li>Apple</li><li>Banana</li><li>Cherry</li></ul>
C<ul><li>0</li><li>1</li><li>2</li></ul>
D<ul><li>fruit</li><li>fruit</li><li>fruit</li></ul>
Attempts:
2 left
💡 Hint

Remember that map returns a new array of elements, and React renders each element inside the list.

📝 Syntax
intermediate
2:00remaining
Which option correctly uses map to double numbers in React JSX?

You want to display a list of doubled numbers from an array [1, 2, 3]. Which code snippet correctly uses map inside JSX?

A{[1, 2, 3].map(num => &lt;div key={num}&gt;{num * 2}&lt;/div&gt;)}
B{[1, 2, 3].map(num =&gt; &lt;div key&gt;{num * 2}&lt;/div&gt;)}
C{[1, 2, 3].map(num =&gt; &lt;div&gt;num * 2&lt;/div&gt;)}
D{[1, 2, 3].map(num) =&gt; &lt;div&gt;{num * 2}&lt;/div&gt;}
Attempts:
2 left
💡 Hint

Check for correct arrow function syntax and proper use of key attribute.

🔧 Debug
advanced
2:00remaining
Why does this React component throw a warning about keys?

Look at this component:

function Colors() {
  const colors = ['red', 'green', 'blue'];
  return (
    <div>
      {colors.map(color => <span>{color}</span>)}
    </div>
  );
}

What is the cause of the warning in the browser console?

React
function Colors() {
  const colors = ['red', 'green', 'blue'];
  return (
    <div>
      {colors.map(color => <span>{color}</span>)}
    </div>
  );
}
AThe map function is missing a return statement.
BThe 'color' variable is not defined inside the map function.
CReact components cannot return multiple elements.
DEach child in a list should have a unique 'key' prop.
Attempts:
2 left
💡 Hint

React requires a special prop when rendering lists to track items.

state_output
advanced
2:00remaining
What is the output after clicking the button in this React component?

Consider this component that uses map and state:

import { useState } from 'react';

function CounterList() {
  const [counts, setCounts] = useState([0, 0, 0]);

  function incrementFirst() {
    const newCounts = counts.map((count, index) => index === 0 ? count + 1 : count);
    setCounts(newCounts);
  }

  return (
    <div>
      {counts.map((count, i) => <p key={i}>Count {i}: {count}</p>)}
      <button onClick={incrementFirst}>Increment First</button>
    </div>
  );
}

What will be displayed after clicking the button once?

React
import { useState } from 'react';

function CounterList() {
  const [counts, setCounts] = useState([0, 0, 0]);

  function incrementFirst() {
    const newCounts = counts.map((count, index) => index === 0 ? count + 1 : count);
    setCounts(newCounts);
  }

  return (
    <div>
      {counts.map((count, i) => <p key={i}>Count {i}: {count}</p>)}
      <button onClick={incrementFirst}>Increment First</button>
    </div>
  );
}
A
Count 0: 0
Count 1: 0
Count 2: 1
B
Count 0: 0
Count 1: 1
Count 2: 0
C
Count 0: 1
Count 1: 0
Count 2: 0
D
Count 0: 1
Count 1: 1
Count 2: 1
Attempts:
2 left
💡 Hint

Check which index is incremented in the map function.

🧠 Conceptual
expert
2:00remaining
Why is using array index as key in map sometimes problematic?

In React, when rendering lists with map, developers often use the array index as the key prop. Why can this cause issues?

AUsing index as key can cause incorrect component reuse when list items change order or are added/removed.
BReact requires keys to be strings, so numbers like index cause errors.
CKeys must be unique across the entire app, so index keys cause conflicts.
DUsing index as key slows down rendering because React re-renders all items every time.
Attempts:
2 left
💡 Hint

Think about what happens when the list changes dynamically.