Alternative Solution to this problem?


1. Why must we generate from 1 to 5 for variable damageThisRound, we just need 1,2,3,4 since there are only 4 hits. instead of if (totalDamage >=4), this could be if (totalDamage =4)
2. Why must we add another variable "totalDamage"? we only need "damageThisRound" as seen from my code since totalDamage is set to 0?

Thank you for your help.

var slaying = true;
var youHit = Math.floor(Math.random()*2);
var damageThisRound = Math.floor(Math.random()*4+1);

while(slaying) {
    if (youHit) { console.log ("Yout hit the dragon");
    if (damageThisRound = 4) {
        console.log("You slew the dragon");
        slaying =false;}
        else {youHit = Math.floor(Math.random()*2);
    } else {console.log("The dragon defeated you");
slaying = false;


Now, there is only a small change the random number is 4, while if it can be 4 and 5, you have a better change of beating the dragon. this:

if (totalDamage =4)

is a particular bad idea, now you always win (you assign a value of 4 to totalDamage, positive integers are considered true), you might want to use two equal signs instead (or even better, use >=)

I am slightly confused, but i think this game build in totalDamage, to offer you an opportunity, to expand the game (but is not part of the tutorial). if you could hit multiply times, you most certainly want to have >= 4, since 3 + 2 = 5, which is greater then 4, if you use ==, then 5 == 4 is false, so you loose, while you killed the dragon


As far as I've understand it, totalDamage is kind of like the dragons HP, so you do damage each round and once the damage exceed a threshold you're done. I've no idea why this is done by accumulating damage instead of decreasing a dragonHP variable but I guess that was the idea.


guess so, but you can also just do one hit, so it seems a bit pointless


Yes if you draw a 4 or even 5 for damageThisRound it is a one hit, if you're lucky with the youHit as well, but for a 1 it could get pretty hard for your luck to be effective :slight_smile:


thanks for your help :slight_smile: