Assigned to: Group 7 (or combine with another group)
Estimated time: 20 minutes
Difficulty: ⭐⭐ Medium
Create a brand new function calculateAverageHP that calculates the average HP of Pokemon in a list. Use TDD approach.
- ✍️ Write the tests FIRST
- 💻 Create the function
- ✅ Make tests pass
- 🧪 Add edge cases
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);
});
});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
};import {
filterByType,
getPokemonNames,
getStrongestPokemon,
sortByName,
calculateAverageHP // Add this!
} from '../pokemon-utils.js';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- Tests written first (TDD)
- Function implemented correctly
- Handles edge cases (empty array, single item)
- All tests pass
- Function exported and imported properly