Plz Help in this code



I am working on the following problem set on CodeWars for 2 hours now:

Create a function that returns the name of the winner in a fight between two fighters.

Each fighter takes turns attacking the other and whoever kills the other first is victorious. Death is defined as having health <= 0.

Each fighter will be a Fighter object/instance.

Both health and damagePerAttack (damage_per_attack for python) will be integers larger than 0. You can mutate the Fighter objects.

This is the best and the most efficient code I came up with:

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

function declareWinner(fighter1, fighter2, firstAttacker) {
      if( > {
      } else {
      if(fighter2.damagePerAttack === fighter1.damagePerAttack) return;

I can’t pass all the random tests that it puts my code through. Can’t see what I’m missing…

I divided the fighter’s health with the other fighter’s damage. Whoever needs more turns to reach 0 health will win.

I think the problem is in the last line of the function. If both fighter’s damages are equal but what if their healths are not?


If you include (link to) the kata, we can at least see the tests its failing




this line:

if(fighter2.damagePerAttack === fighter1.damagePerAttack) return;

will never execute, given here:

      } else {

one of the fighters name is returned, which will end the function

Among other problems


I put the line at the top of the function and even then there are errors…


And what are the implication to your program when doing so? Why are you comparing damagePerAttack? Higher damage does mean you win automatically, the other might have a lot more health


it seems you are trying something clever, but this code will keep corner cases for example:

Fighter {
  name: 'Mark',
  health: 88,
  damagePerAttack: 73,
  toString: [Function] } Fighter {
  name: 'Willy',
  health: 311,
  damagePerAttack: 19,
  toString: [Function] } 'Willy'

willy is first to attack, so he should win, but if we do the math:

>>> 311/73

>>> 88/19

we see that mark has a higher ratio, but he should loose.


Well, initially I assumed that every fighter had health 10. But nowhere in the instruction it is written. It’s written that both health and damagePerAttack should be integers larger than 0.

You’re right. But the question in my mind was what if Fighter1 has a health of 10 and damage 5 and Fighter2 has health 30 and damage 3.

Fighter1 will need many more turns to beat Fighter2 and Fighter2 will defeat Fighter1 in like 4 attacks…


this is just tedious, i looked at the solution which suggested using Math.ceil() to overcome the corner cases float comparison


Yeah you’re right. You’re also no less clever. I think I’m having problem in analysing the solution…




This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.