codex-lv3-may-2025

Kata 5: Fruit List

Concept: List pattern - displaying arrays in JSX

Challenge

Create a FruitList component that:

  1. Has an array of fruits: ['Apple', 'Banana', 'Orange', 'Grape']
  2. Displays them as an unordered list (<ul>) with list items (<li>)
  3. Uses a for loop to build an array of JSX elements

🔗 Practice on CodeSandbox

Expected Output

• Apple
• Banana
• Orange
• Grape

Starter Code

export default function FruitList() {
  const fruits = ['Apple', 'Banana', 'Orange', 'Grape'];
  
  // Build array of list items using a for loop
  
  return (
    <ul>
      {/* Display the array of list items here */}
    </ul>
  );
}

Hints

Solution

Click to reveal solution
export default function FruitList() {
  const fruits = ['Apple', 'Banana', 'Orange', 'Grape'];
  
  // Build list items using a for loop
  const listItems = [];
  for (let i = 0; i < fruits.length; i++) {
    listItems.push(<li key={i}>{fruits[i]}</li>);
  }
  
  return (
    <ul>
      {listItems}
    </ul>
  );
}
**Note:** You can also use `.map()` as a shortcut:
{fruits.map((fruit, index) => <li key={index}>{fruit}</li>)}

Concept Review

Note: Learn more about the built-in .map() method at MDN: Array.map()


Back to Kata Index