Skip to content

Latest commit

 

History

History
28 lines (25 loc) · 501 Bytes

8 kyu - Grasshopper - Terminal game combat function.md

File metadata and controls

28 lines (25 loc) · 501 Bytes

Task

Create a combat function that takes the player's current health and the amount of damage recieved and returns the player's new health. Health can't be less than 0.

My solution

def combat(health, damage)
  if health - damage > 0
    return health - damage
  else
    return 0
  end
end

Factored solution

def combat(health, damage)
  health - damage > 0 ? health - damage : 0
end

Better Solution

def combat(health, damage)
  [health-damage,0].max
end