Bird
Raised Fist0
Reactframework~20 mins

Map function usage in React - Practice Problems & Coding Challenges

Choose your learning style10 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.

Practice

(1/5)
1. What is the main purpose of using the map function in React when rendering lists?
easy
A. To mutate the original array directly
B. To remove duplicate elements from the array
C. To sort the array elements alphabetically
D. To transform an array into a list of React elements

Solution

  1. Step 1: Understand what map does in JavaScript

    The map function creates a new array by applying a function to each element of the original array without changing it.
  2. Step 2: Apply this to React rendering

    In React, map is used to convert each item in an array into a React element, which can then be rendered as a list.
  3. Final Answer:

    To transform an array into a list of React elements -> Option D
  4. Quick Check:

    map creates React elements from arrays = B [OK]
Hint: Remember: map creates new arrays, perfect for rendering lists [OK]
Common Mistakes:
  • Thinking map changes the original array
  • Confusing map with sorting or filtering
  • Not realizing map returns a new array
2. Which of the following is the correct syntax to use map inside a React component to render a list of names stored in const names = ['Anna', 'Bob', 'Cara']?
easy
A. {names.map(name => <li key={name}>{name}</li>)}
B. {names.map(name => <li>name</li>)}
C. {names.forEach(name => <li key={name}>{name}</li>)}
D. {names.map(name => <li>{name}</li> key={name})}

Solution

  1. Step 1: Check the correct use of map and JSX

    Using map with an arrow function returning JSX elements is correct. The element must have a unique key prop inside the returned JSX.
  2. Step 2: Identify the correct syntax

    {names.map(name => <li key={name}>{name}</li>)} correctly uses map, returns a list item with curly braces around name, and includes a unique key prop.
  3. Final Answer:

    {names.map(name => <li key={name}>{name}</li>)} -> Option A
  4. Quick Check:

    map + JSX + key prop = A [OK]
Hint: Always add a unique key prop inside map JSX elements [OK]
Common Mistakes:
  • Using forEach instead of map
  • Missing curly braces around variables in JSX
  • Placing key prop outside the JSX element
3. What will be the output of this React code snippet?
const numbers = [1, 2, 3];
const listItems = numbers.map(n => <li key={n}>Number: {n * 2}</li>);
return <ul>{listItems}</ul>;
medium
A.
  • Number: 2
  • Number: 4
  • Number: 6
B. SyntaxError due to missing parentheses
C.
  • Number: n * 2
  • Number: n * 2
  • Number: n * 2
D.
  • Number: 1
  • Number: 2
  • Number: 3

Solution

  1. Step 1: Understand the map transformation

    Each number in the array is multiplied by 2 inside the JSX expression: {n * 2}.
  2. Step 2: Predict the rendered list items

    The list items will show "Number: 2", "Number: 4", and "Number: 6" respectively.
  3. Final Answer:

    <ul><li>Number: 2</li><li>Number: 4</li><li>Number: 6</li></ul> -> Option A
  4. Quick Check:

    n * 2 applied in map = D [OK]
Hint: Check the expression inside map carefully for output values [OK]
Common Mistakes:
  • Assuming original numbers are shown without multiplication
  • Confusing JSX expression syntax
  • Thinking map returns strings instead of elements
4. Identify the error in this React code using map:
const items = ['a', 'b', 'c'];
return <ul>{items.map(item => <li>{item}</li>)};
medium
A. map function cannot be used inside JSX
B. Missing unique key prop on <li> elements
C. Incorrect use of curly braces around item
D. Array items must be numbers, not strings

Solution

  1. Step 1: Check JSX inside map

    The map function returns
  2. elements but none have a key prop, which React requires for lists.
  3. Step 2: Confirm other syntax is correct

    Curly braces around item are correct, map is valid inside JSX, and array can contain strings.
  4. Final Answer:

    Missing unique key prop on <li> elements -> Option B
  5. Quick Check:

    React needs keys in lists = C [OK]
Hint: Always add key prop to elements inside map [OK]
Common Mistakes:
  • Ignoring missing key warnings
  • Thinking keys are optional
  • Confusing key prop placement
5. Given this nested array of user objects:
const users = [
  { id: 1, name: 'Alice', hobbies: ['Reading', 'Hiking'] },
  { id: 2, name: 'Bob', hobbies: ['Cooking'] }
];

// Which code correctly renders a list of users with their hobbies?
hard
A. {users.map(user => <div key={user.id}><h3>{user.name}</h3><ul>{user.hobbies.map(hobby => <li key={user.id}>{hobby}</li>)}
B. {users.map(user => <div><h3>{user.name}</h3><ul>{user.hobbies.map(hobby => <li>{hobby}</li>)}
C. {users.map(user => <div key={user.id}><h3>{user.name}</h3><ul>{user.hobbies.map(hobby => <li key={hobby}>{hobby}</li>)}
D. {users.map(user => <div key={user.name}><h3>{user.name}</h3><ul>{user.hobbies.map(hobby => <li key={user.id}>{hobby}</li>)}

Solution

  1. Step 1: Check keys on outer and inner lists

    The outer div uses user.id as key, which is unique. The inner li uses hobby as key, which is unique per hobby.
  2. Step 2: Verify correct JSX structure and keys

    {users.map(user => <div key={user.id}><h3>{user.name}</h3><ul>{user.hobbies.map(hobby => <li key={hobby}>{hobby}</li>)} correctly nests the hobbies list inside each user and assigns proper keys. Other options miss keys or reuse keys incorrectly.
  3. Final Answer:

    {users.map(user => <div key={user.id}><h3>{user.name}</h3><ul>{user.hobbies.map(hobby => <li key={hobby}>{hobby}</li>)} -> Option C
  4. Quick Check:

    Unique keys on nested lists = A [OK]
Hint: Use unique keys for both outer and inner mapped elements [OK]
Common Mistakes:
  • Reusing same key for multiple elements
  • Missing key on inner map elements
  • Using non-unique keys like user name or id incorrectly
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard