Skip to content

Instantly share code, notes, and snippets.

@manavm1990
Created October 21, 2025 00:04
Show Gist options
  • Select an option

  • Save manavm1990/8d61da2eda63f9ae52ff8430955ba3f8 to your computer and use it in GitHub Desktop.

Select an option

Save manavm1990/8d61da2eda63f9ae52ff8430955ba3f8 to your computer and use it in GitHub Desktop.

Ticket #4: Add calculateAverageHP

Assigned to: Group 7 (or combine with another group)
Estimated time: 20 minutes
Difficulty: ⭐⭐ Medium

What You're Doing

Create a brand new function calculateAverageHP that calculates the average HP of Pokemon in a list. Use TDD approach.

Tasks

  1. ✍️ Write the tests FIRST
  2. 💻 Create the function
  3. ✅ Make tests pass
  4. 🧪 Add edge cases

Step 1: Write Tests First

describe('calculateAverageHP', () => {
  test('should calculate average HP correctly', () => {
    const pokemon = [
      { name: "Pikachu", hp: 35 },
      { name: "Charmander", hp: 39 },
      { name: "Squirtle", hp: 44 }
    ];
    
    const result = calculateAverageHP(pokemon);
    
    // Average of 35, 39, 44 = 39.33...
    expect(result).toBeCloseTo(39.33, 1);
  });

  test('should return 0 for empty array', () => {
    const result = calculateAverageHP([]);
    expect(result).toBe(0);
  });

  test('should handle single Pokemon', () => {
    const result = calculateAverageHP([{ hp: 100 }]);
    expect(result).toBe(100);
  });
});

Step 2: Create Function

Add to pokemon-utils.js:

export const calculateAverageHP = (pokemonList) => {
  // Hint 1: Check for empty array first
  // Hint 2: Use reduce to sum all HP values
  // Hint 3: Divide by array length
  
  // Your code here
};

Step 3: Import in Test File

import { 
  filterByType, 
  getPokemonNames, 
  getStrongestPokemon,
  sortByName,
  calculateAverageHP  // Add this!
} from '../pokemon-utils.js';

Git Workflow

git checkout -b ticket-4-average-hp

git commit -m "test: add calculateAverageHP tests"
git commit -m "feat: implement calculateAverageHP function"
git commit -m "test: add edge cases for empty array and single Pokemon"

git push origin ticket-4-average-hp

Success Criteria

  • Tests written first (TDD)
  • Function implemented correctly
  • Handles edge cases (empty array, single item)
  • All tests pass
  • Function exported and imported properly
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment