Lesson 2 Dragon slayer "Save & Submit Button is not working"only for this excersise


#1



var slaying=true
youHit = Math.floor(Math.random()*2);
var damageThisRound = Math.floor(Math.random()*5 + 1)
var totalDamage = 0
while(slaying) {
    if (youHit) {
        console.log("You hit!");
        totalDamage=+damageThisRound
        if (totalDamage>=4) {
            console.log("Dragon has croaked, congrats you just killed more wildlife, your ansestors must be pround with you for making another species extinct!");
            slaying=true;
        }else{
            youHit=Math.floor(Math.random()*2);
        }
    } else {
        console.log("You missed!");
    }
    slaying = true;
}


#2

All the 'Save and Submit' buttons in "Dragon Slayer" are working for me. It could be something wrong with your browser?


#3

Thanks for crashing my browser :smile:.

The problem is that you have an infinite loop. Your while loop will keep on going while slaying is set to true, but at the end of the loop, you set slaying = true. It will thus run indefinitely!

That's the cause. The solution is up to you :wink:


#4

Now why didn't I think of that?


#5

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

while(slaying) {
if(youHit == 1) {
console.log("You have hit the Dragon!")
totalDamage += damageThisRound;

    if(totalDamage >= 4) {
       console.log("You have slain the Dragon! You have won!");
       slaying = false;
    }
    else {
       youHit = Math.floor(Math.random() * 2);
    }
}
else {
    console.log("The Dragon has slain you!")
}
slaying = false;

}
slaying = true;
totalDamage = 0;
youHit = 0;