JSFiddle - React, Tailwind, and code Playground

by hescano

JavaScript

function declareWinner(fighter1, fighter2, firstAttacker) {
  var attacker = fighter1.name === firstAttacker ? fighter1 : fighter2;
  var punished = fighter1.name === firstAttacker ? fighter2 : fighter1;
  var tmp;
  while (fighter1.health > 0 && fighter2.health > 0) {
  	punished.health -= attacker.damagePerAttack;
    tmp = attacker;
    attacker = punished;
    punished = tmp;
  }
  
  return (fighter1.health > 0 ? fighter1 : fighter2).toString();
}

function Fighter(name, health, damagePerAttack) {
        this.name = name;
        this.health = health;
        this.damagePerAttack = damagePerAttack;
        this.toString = function() { return this.name; }
}

console.log(declareWinner(new Fighter("Jerry", 30, 3), new Fighter("Harald", 20, 5), "Harald"))