Solved: "totalDamage = totalDamange + damageThisRound" what does this code do?

#1

``Dragon Slayer Exercise 5 The second 'if' statement``

The code is correct but I have trouble understanding what does this code do>> "totalDamage = totalDamange + damageThisRound"

Something about racking up your total hit points but I don't see the logic...

``````var slaying = true;
var youHit = Math.floor(Math.random() * 2);
//if you hit, true (1) or false (0)
var damageThisRound = Math.floor(Math.random() * 5);
var totalDamage = 0;

while (slaying){
if (youHit===1){
console.log("yay you hit the dragon");
totalDamage = totalDamange + damageThisRound;
if (totalDamage >= 4){
console.log("you killed the dragon");
slaying = false;
}
else {
youHit=Math.floor(Math.random() * 2);
}
} else{
console.log("dragon got chu");
}
slaying = false;}``````

#2

Basically `youHit` will generate random damage points to attack the dragon.

`While` will loop `youHit` until you've reached a total number of at least 4 or more damage points.

Until you've reached 4 damaged points and killed the dragon, your `while` loop will either try generating new damage points, or else it's the dragon that hits you and you die.

`totalDamage` will just record the damage you did each round in the loop. When `totalDamage` records at least 4 damage points, it triggers "you killed the dragon" and the game is over.

So what the loop really is saying is "While you're fighting the dragon, if you manage to hit it you get a number of points. If this number of points reaches 4, you kill the dragon. Else, you do not manage to hit it, and it kills you."

#3

Yes thank you I get it now!!

But I think you mean "damgeThisRound" instead of "youHit". Since you determine if you the dragon first with "youHit" (true or false), and then determine how many points you scored with "damgeThisRound".

Thanks again!

#4

Please mark this post as solved.

#5

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.