Problem
- Create a combat function that takes the player’s current health and the amount of damage received, and returns the player’s new health.
- 플레이어의 현재 health와 받은 damage를 사용해서, 새로운 health를 반환한다.
- Health can’t be less than 0.
Solution 01
function combat(health, damage) {
if (health - damage < 0) {
return 0;
} else {
return health - damage;
}
}
combat(100, 5); // 95
combat(100, 100); // 0
combat(100, 120); // 0
Solution 02
function combat(health, damage) {
if (health < damage) {
return 0;
} else {
return health - damage;
}
}
combat(100, 5); // 95
combat(100, 100); // 0
combat(100, 120); // 0
Solution 03
function combat(health, damage) {
return health < damage ? 0 : health - damage;
}
combat(100, 5); // 95
combat(100, 100); // 0
combat(100, 120); // 0