Drill

ProblemsJavaScript › games

Calculate combat damage

easygamesJavaScript

A simple combat system subtracts the defender's rating from the attacker's, but damage never drops below zero.

combatRound(attack: int, defense: int) → int

Solve it in the editor →

Where you start

function combatRound(attack, defense) {
  
}

Worked examples

CallResult
combatRound(10, 5)5
combatRound(3, 7)0
combatRound(0, 0)0
combatRound(100, 100)0

Hint

Subtract, then clamp.

Reference solution in JavaScript
function combatRound(attack, defense) {
  return Math.max(0, attack - defense);
}

The same problem in another language

More games problems in JavaScript