codex-lv3-may-2025

Kata 9: Calculate Total Score

Concept: Reduce pattern to sum numbers (using for loop)

Challenge

Create a TotalScore component that:

  1. Has an array of scores: [85, 92, 78, 90, 88]
  2. Calculates the total sum of all scores using a for loop
  3. Displays the individual scores and the total

🔗 Practice on CodeSandbox

Expected Output

Scores: 85, 92, 78, 90, 88
Total: 433

Starter Code

export default function TotalScore() {
  const scores = [85, 92, 78, 90, 88];
  
  // Calculate total using a for loop
  
  return (
    <div>
      {/* Display scores and total */}
    </div>
  );
}

Hints

Solution

Click to reveal solution
export default function TotalScore() {
  const scores = [85, 92, 78, 90, 88];
  
  // REDUCE: Sum all scores using a for loop
  let total = 0;
  for (let i = 0; i < scores.length; i++) {
    total = total + scores[i];
  }
  
  return (
    <div>
      <p>Scores: {scores.join(', ')}</p>
      <p>Total: {total}</p>
    </div>
  );
}

Concept Review

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

Challenge Variation

Try calculating:


Back to Kata Index