codex-lv3-may-2025

Kata 11: Count Items by Category

Concept: Reduce pattern to count (using for loop)

Challenge

Create a CategoryCount component that:

  1. Has an array of items with categories:
       const items = [
      { name: 'Apple', category: 'Fruit' },
      { name: 'Carrot', category: 'Vegetable' },
      { name: 'Banana', category: 'Fruit' },
      { name: 'Broccoli', category: 'Vegetable' },
      { name: 'Orange', category: 'Fruit' }
    ];
    ```
    
  2. Counts how many items are in each category using a for loop
  3. Displays the counts

šŸ”— Practice on CodeSandbox

Expected Output

</code></pre> Fruit: 3 Vegetable: 2


## Starter Code

jsx export default function CategoryCount() { const items = [ { name: ā€˜Apple’, category: ā€˜Fruit’ }, { name: ā€˜Carrot’, category: ā€˜Vegetable’ }, { name: ā€˜Banana’, category: ā€˜Fruit’ }, { name: ā€˜Broccoli’, category: ā€˜Vegetable’ }, { name: ā€˜Orange’, category: ā€˜Fruit’ } ];

// Count items by category using a for loop

return ( <div> {/* Display counts */} </div> ); }


## Hints

- Create variables to count each category: `let fruitCount = 0;`
- Loop through items and increment the appropriate counter
- Use if statements to check category

## Solution

<details>
<summary>Click to reveal solution</summary>

jsx export default function CategoryCount() { const items = [ { name: ā€˜Apple’, category: ā€˜Fruit’ }, { name: ā€˜Carrot’, category: ā€˜Vegetable’ }, { name: ā€˜Banana’, category: ā€˜Fruit’ }, { name: ā€˜Broccoli’, category: ā€˜Vegetable’ }, { name: ā€˜Orange’, category: ā€˜Fruit’ } ];

// REDUCE: Count items by category using a for loop let fruitCount = 0; let vegetableCount = 0;

for (let i = 0; i < items.length; i++) { if (items[i].category === ā€˜Fruit’) { fruitCount = fruitCount + 1; } else if (items[i].category === ā€˜Vegetable’) { vegetableCount = vegetableCount + 1; } }

return ( <div> <p>Fruit: {fruitCount}</p> <p>Vegetable: {vegetableCount}</p> </div> ); } ```

</details>

Concept Review

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

Challenge Variation

Try:


← Back to Kata Index