Plz Help in this code

js

#1

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) {
        this.name = name;
        this.health = health;
        this.damagePerAttack = damagePerAttack;
        this.toString = function() { return this.name; }
} //Fighter class in JS

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

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?


#2

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


#3

Here: https://www.codewars.com/kata/two-fighters-one-winner/train/javascript


#4

this line:

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

will never execute, given here:

return fighter1.name
      } else {
      return fighter2.name
      } 

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

Among other problems


#5

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


#6

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


#7

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
4.260273972602739

>>> 88/19
4.631578947368421

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


#8

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…


#9

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


#10

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


#11

Okay…


#12

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