Dragon Slayer 6/6 Eternal Loop?


#1

Hello People,

I still cannot find the mistake in my code, but the programm ends sometimes in an eternal loop:

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

while(slaying){
if(youHit){
    totalDamage += damageThisRound;
    console.log("damageThisRound: " + damageThisRound + " totalDamage: " + totalDamage);
    if(totalDamage >= 4){
        console.log("Congratulations, you slayed dat one with " + totalDamage + " damage dealt!");
    }
    else{
        damageThisRound = Math.floor(Math.random()*4 + 1);
        totalDamage += damageThisRound;
        console.log("damageThisRound#2: " + damageThisRound + " totalDamage#2: " + totalDamage);
        if(totalDamage >= 4){
            console.log("Congratulations, you had luck with your second swing and slayed dat one with " + totalDamage + " damage dealt!");
        }
        else{
            console.log("The D was stronger than you, cause you are weak and dealt only " + totalDamage + " damage!");
        }
    }  
slaying = false;
}
}

If someone could help me, it would be awesome :smile:


#2

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

while (slaying) {
if (youHit) {
console.log("damageThisRound: " + damageThisRound + " totalDamage: " + totalDamage);
totalDamage += damageThisRound;

if (totalDamage >= 4) {
  console.log("Congratulations, you slayed dat one with " + totalDamage + " damage dealt!");
  slaying = false;
} else {
  youHit = Math.floor(Math.random() * 2);
}

} else {
console.log("The D was stronger than you, cause you are weak and dealt only " + totalDamage + " damage!");
slaying = false;
}
}