DEV Community

dev.to staff
dev.to staff

Posted on

Daily Challenge #28 - Kill the Monster!

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 (11)

Collapse
 
choroba profile image
E. Choroba

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.

#!/usr/bin/perl
use warnings;
use strict;

use Function::Parameters;

fun kill_monsters($h, $m, $dm) {
    my $hits   = int(($m - 1) / 3);
    my $damage = $dm * $hits;
    $h -= $damage;
    return $h > 0
        ? "hits: $hits, damage: $damage, health: $h"
        : 'hero died'
}

use Test::More tests => 3;

is kill_monsters(100, 3, 33), 'hits: 0, damage: 0, health: 100';
is kill_monsters( 50, 7, 10), 'hits: 2, damage: 20, health: 30';
is kill_monsters(  2, 7,  1), 'hero died';
Collapse
 
wheatup profile image
Hao

Solution in javascript:

const killMonsters = (h, m, dm) => {
    const hits = Math.floor((m - 1) / 3);
    const damage = hits * dm;
    const health = h - damage;
    return health <= 0 ? 'Hero died' : `hits: ${hits}, damage: ${damage}, health: ${health}`;
}
Collapse
 
nishu1343 profile image
nishu1343

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;

Collapse
 
brightone profile image
Oleksii Filonenko • Edited

Elixir:

defmodule Monsters do
  def kill(health, monsters, damage_per_hit) do
    hits = div(monsters - 1, 3)
    total_damage = damage_per_hit * hits
    result(hits, total_damage, health - total_damage)
  end

  defp result(hits, damage, health) when health > 0,
    do: "hits: #{hits}, damage: #{damage}, health: #{health}"

  defp result(_, _, _), do: "hero died"
end
Collapse
 
matrossuch profile image
Mat-R-Such

Python:

def killMonsters(h,m,dm):
    if m <= 3 :     print('hits: %d, damage: %d, health: %d' %(0,0,h))
    else:
        k= m // 4
        if k*dm >= h:    print('Hero Died')
        else: print('hits: %d, damage: %d, health: %d' %(k,k*dm,h-(k*dm)))
Collapse
 
ianfabs profile image
Ian Fabs • Edited

I did it!

The following is my solution in javascript:

function killMonsters(h, m, dm) {
  let health = h, mh = Math.floor(m/3), hd = mh * dm;
  health -= hd;
  console.log(
    health <= 0
    ? 
    "Hero died"
    : 
    `hits: ${mh}, damage: ${hd}, health: ${health}`
  );
}

killMonsters(100, 3, 33);
killMonsters(50, 7, 10);
Collapse
 
choroba profile image
E. Choroba • Edited

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.

Collapse
 
alvaromontoro profile image
Alvaro Montoro • Edited

JavaScript

const killMonsters = (health, monsters, damage) => 
                         damage * Math.floor((monsters-1) / 3) >= health 
                         ? "hero died" 
                         : `hits: ${Math.floor((monsters-1) / 3)}, damage: ${Math.floor((monsters-1) / 3) * damage}, health: ${health - Math.floor((monsters-1) / 3) * damage}`;

Live demo on CodePen.

Collapse
 
nishu1343 profile image
nishu1343

I didnot understand this brother. Could u clarify please.

damage * Math.floor((monsters-1) / 3) >= health