Problem: game combat function

Having trouble with this problem. Don’t understand how it is not taking my code. I am telling it that hp is health - damage and if hp is less than 0, return Health cannot go below 0

function combat(health, damage) {
  const hp = health - damage;
  console.log(hp);
  if (hp > 0) {
  return hp;
    }
  else {
    return 'Health cannot go below 0';
  }
}

This is not what your code is saying. Your code is written is if hp is greater than 0

sorry!! typo. I meant that if it’s 0 or greater than 0, return amount of hp

function combat(health, damage) {
  const hp = health - damage;
  console.log(hp);
  if (hp === 0 || hp > 0) {
  return hp;
    }
  else {
    return 'Health cannot go below 0';
  }
}

So what exactly is the issue? Where are you calling the function?

I put your code in a codepen and it is doing what your code is telling it to do

function combat(health, damage) {
  const hp = health - damage;
  if (hp > 0) {
    return hp;
  } else {
    return 0;
  }
}

passis all test

ok SEE…THAT was the problem…I wasn’t returning 0…it didn’t say anything anywhere to return 0, it just says ’ Health can’t be less than 0’.

function combat(health, damage) {
  const hp = health - damage;
  console.log(hp);
  if (hp > 0) {
  return hp;
    }
  else {
    return 'Health cannot go below 0';
  }
}

Thank you for the codewars link. I would still play, but I’m currently learning Python. I don’t have time to brainstorm. My motto is “simplicity” in everything.

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