Code prints out incorrect statement and occasionally runs an infinite loop


#1

Here is my code:

var slaying = true
var youHit = Math.floor(Math.random() * 2)
var damageThisRound = Math.floor(Math.random() * 5 + 1)
var totalDamage = 0
while(slaying){
    console.log("You are fighting a dragon!");
    if(youHit){
        console.log("You swing and kill the dragon!")
        totalDamage+=damageThisRound;
        if(totalDamage>=4){
            youHit = Math.floor(Math.random() * 2);
    }   else{
            console.log("You swing and miss the dragon! You die!")
    }       slaying = false;
    }
}

It will sometimes print out "You swing and kill the dragon" and "You swing and miss the dragon. You die!"
I would like it to only print one or the other.

And sometimes a number will be generated that forces the while loop to run continuously and crash. But only sometimes. Why is that, and how can I address it?


#2

don't mean to bump, but after a good 30mins trying to figure out what might be wrong, I still can't figure it out
EDIT: Added my second else statement (should have been there?) but it seems to loop infinitely


#3

Hi,

This is my first attempt to try to explain - so please bear with me - but I believe you're missing an else statement and you need to close your loop to address the infinite loop situation with "slaying=false", probably due to the missing else.

This might also address coding issues, be aware of your punctuations: brackets and semicolons.

As far as your quotes not printing how you'd like, pay attention to how if/else nests and that might help. This is difficult because the Math make understanding it more elusive. In this case, I would look at how the loop is closed. I'm certainly still learning the ins and outs of this exercise but this is my take on the problems you wanted addressed.

Again, this is my first time but thought I would contribute and give my two cents and hope this helps. So call out to all the coding vets, please correct me if I'm wrong :grin:

Here's my solution for your code:

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

while(slaying){
    console.log("You are fighting a dragon!");  

        if(youHit){
            totalDamage+=damageThisRound;

            if(totalDamage>=4){
                console.log("You swing and kill the dragon!");          
                slaying=false;                                          

            } else{                                                                     
                youHit = Math.floor(Math.random() * 2);                 
        }

    } else{
            console.log("You swing and miss the dragon! You die!");
            slaying = false;
    }
}

here's my dragonslayer for comparison:

    var slay = true;                               
    var hit = Math.floor(Math.random() * 2);
    var damage = Math.floor(Math.random() * 5 + 1);
    var damageTotal = 0;                      

    while (slay) {                                   
        console.log("Twas brillig, and the slithy toves...");
      if (hit) {
        damageTotal += damage;            
        console.log("One, two, one, two, with " + damageTotal + " and through!");
            if (damageTotal >= 4) {
                console.log("O frabjous day! Callooh! Callay!");
                slay = false; 
            } else {
                hit = Math.floor(Math.random() * 2);
            }
      } else {
        console.log("The manxome foe prevails.");
        slay = false;
      }
    }

plug both into Codecamey labs and see the differences.