Trying to use the ternary operator to display the value of a variable if the variable is greater than a given number

155 Views Asked by At

I am going through the JavaScript curriculum of FreeCodeCamp and I am stuck trying to get a return value for the 'hit' variable by using a ternary operator.

What FCC wants me to do to pass the challenge:

In getMonsterAttackValue, change return hit to a ternary operator that returns hit if hit is greater than 0, or returns 0 if it is not.

function getMonsterAttackValue(level) {
  const hit = (level * 5) - (Math.floor(Math.random() * xp));
  console.log(hit); 
  return hit > 0 ? 'hit' : '0'
  
}
1

There are 1 best solutions below

0
Nina Scholz On

You could simply take the larger value with Math.max.

return Math.max(hit, 0);