Alternative Solution to this problem?


#1


https://www.codecademy.com/en/courses/javascript-beginner-en-mrTNH-6VIZ9/0/5?curriculum_id=506324b3a7dffd00020bf661


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;
}
}


#2

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


#3

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.


#4

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


#5

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:


#9

thanks for your help :slight_smile: