We have a real challenge for you today - You are fighting against a monster and are strong enough to kill it with a few hits. But after every 3 punches you make, the monster hits you once. Your health is $h; number of monsters is $m, damage that monster can give you is $dm.
Write a function that will calculate: how many hits you received, how much damage you received and your remaining health. If your health is <= 0, you die and function should return "hero died".
Examples
killMonsters(100, 3, 33); // => "hits: 0, damage: 0, health: 100"
killMonsters(50, 7, 10); // => "hits: 2, damage: 20, health: 30"
Note: All numbers are strictly positive. Your function should always return a string.
Good luck!
This challenge comes from user levan765. Thank you to CodeWars, who has licensed redistribution of this challenge under the 2-Clause BSD License!
Want to propose a challenge for a future post? Email yo+challenge@dev.to with your suggestions!
Top comments (9)
I'm not sure I understand the assignment correctly. The damage I give to the monster is always 1, right? So if the monster's health is 3, it gets killed before it can attack back. It needs at least 4 hit points to be able to bite.
Solution in javascript:
Hello Hao, could u explain these 3 lines.ima beginner and trying to understand
const hits = Math.floor((m - 1) / 3);
const damage = hits * dm;
const health = h - damage;
Elixir:
Python:
I did it!
The following is my solution in javascript:
It returns
hits: 1, damage: 33, health: 67
for the first input, not the expected output. But my solution does the same...Update: It doesn't anymore.
JavaScript
Live demo on CodePen.
I didnot understand this brother. Could u clarify please.
damage * Math.floor((monsters-1) / 3) >= health