Problems › TypeScript › games
Calculate combat damage
A simple combat system subtracts the defender's rating from the attacker's, but damage never drops below zero.
- Damage equals attack minus defense.
- A negative result is clamped to zero.
combatRound(attack: int, defense: int) → int
Where you start
function combatRound(attack: number, defense: number): number {
}
Worked examples
| Call | Result |
|---|---|
combatRound(10, 5) | 5 |
combatRound(3, 7) | 0 |
combatRound(0, 0) | 0 |
combatRound(100, 100) | 0 |
Hint
Subtract, then clamp.
Reference solution in TypeScript
function combatRound(attack: number, defense: number): number {
return Math.max(0, attack - defense);
}