6/6 It never says that I have defeated the dragon


#1

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

while(slaying) {
    if(youHit === 1) {
        console.log("You hit the dragon");
    totalDamage += damageThisRound;
        if(totalDamage >= 4) {
            console.log("You have defeated the dragon");
        slaying = false;
        }
        else {
            youHit = Math.floor(Math.random() * 2);
            }
    }
    else {
        console.log("You missed him!");
    }
    slaying = false;
}

My code works and all that but it never says: "You have defeated the dragon". i don't know how to get that. Help me please. BTW if I have some more stuff that isn't good but codecademy accepts it please note it.


#2

This slaying should be inside of the else. Currently you set slaying to false at the end of every loop which leads to leaving the loop at that point as while(false) stops. As you damage is 3 and the required damage is 4 you'll have no chance to win. Also from the game aspect it makes more sense that the 2 options for game over (which is done by setting slaying to false) are either you win by dealing a totalDamage > 3 or by a failed hit when the dragon has the opportunity to strike back.


#3

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

while(slaying) {
    youHit = Math.floor(Math.random() * 2);
    if(youHit === 1) {
        console.log("You hit the dragon");
    totalDamage += damageThisRound;
        if(totalDamage >= 4) {
            console.log("You have defeated the dragon");
        slaying = false;
        }
        else {
            youHit = Math.floor(Math.random() * 2);
            }
    }
    else {
        console.log("You missed him!");
    }
}

This is the new one. Better?


#4

Better, but now you can only win as this is the only option on which you can leave the loop. What I meant is that you should put the slaying =false in the else part:

else {
    console.log("You missed him!");
    slaying = false;
}

Now you loose if you miss and win if you happen to have 2 attacks.


#5

var slaying = true;
var youHit = Math.floor(Math.random() * 2);
var damageThisRound = 3;
var totalDamage = 0;
var playerDamage = 0;
var dragonHit = Math.floor(Math.random() * 2);

while(slaying) {
    
    dragonHit = Math.floor(Math.random() * 2);
    youHit = Math.floor(Math.random() * 2);
    
    if(youHit === 1) { 
        var damage = Math.round(Math.random() * 3 );
        
        if(damage === 0) {
            console.log("you hit the dragon but didn't do any damage");
        }
        else {
            console.log("You dealt " + damage + " to the dragon.");
            totalDamage += damage;
            
            console.log("Dragon damage: " + totalDamage);
            
            if(totalDamage >= 6) {
                console.log("You have defeated the dragon");
                slaying = false;
            }
        }
    }
    else {
        console.log("You missed him!");
    }
    
    if(dragonHit === 1) {
        var damage = Math.round(Math.random() * 3 );
        
        if(damage === 0) {
            console.log("the dragon hit the player but didn't do any damage");
        }
        else {
            playerDamage += damage;
            console.log("The dragon dealt " + damage + " to the player.");
            console.log("Player damage: " + playerDamage);
            
            if(playerDamage >= 6) {
                console.log("You have been defeated");
                slaying = false;
            }
        }
    }
    else {
        console.log("The dragon missed you!");
    }
}

Use this code. I think it's better then the others :slightly_smiling:. Only now i also used Math.round which I haved learned yet but...


#6

Looks nice :thumbsup: but in this case you can get rid of the unused damageThisRound variable :slightly_smiling:
And the other thing that you should keep in mind concerns this:

youHit = Math.floor(Math.random() * 2);
dragonHit = Math.floor(Math.random() * 2);

Math.random outputs a random number (who'd have thought :slightly_smiling: ) but after you got the number this number stays the same. So you probably want to do this in every loop or otherwise you will always hit or always miss based on the result on the top.

PS: Probably something like this:

        if(damage === 0) {
            console.log("you hit the dragon but didn't do any damage");
        }
        else {
            console.log("You dealt " + damage + " to the dragon.");
            totalDamage += damage;
            
            console.log("Dragon damage: " + totalDamage); 
        }
        if(totalDamage >= 6) {
            console.log("You have defeated the dragon");
            slaying = false;
        }
        else{
            youHit = Math.floor(Math.random() * 2); 
        }

#7

var slaying = true;
var youHit = Math.floor(Math.random() * 2);
var damageThisRound = 3;
var totalDamage = 0;
var playerDamage = 0;
var dragonHit = Math.floor(Math.random() * 2);

while(slaying) {
    
    dragonHit = Math.floor(Math.random() * 2);
    youHit = Math.floor(Math.random() * 2);
    
    if(youHit === 1) { 
        var damage = Math.round(Math.random() * 3 );
        
        if(damage === 0) {
            console.log("you hit the dragon but didn't do any damage");
        }
        else {
            console.log("You dealt " + damage + " to the dragon.");
            totalDamage += damage;
            if(totalDamage > 6) {
                totalDamage = 6;
            }
            console.log("Dragon damage: " + totalDamage);
            
            if(totalDamage >= 6) {
                console.log("You have defeated the dragon");
                slaying = false;
                dragonHit = 0;
            }
        }
    }
    else if (playerDamage < 6){
        console.log("You missed the dragon!");
    }
    
    if(dragonHit === 1) {
        var damage = Math.round(Math.random() * 3 );
        
        if(damage === 0) {
            console.log("the dragon hit the player but didn't do any damage");
        }
        else {
            playerDamage += damage;
            if(totalDamage > 6) {
                totDamage = 6;
            }
            console.log("The dragon dealt " + damage + " to the player.");
            console.log("Player damage: " + playerDamage);
            
            if(playerDamage >= 6) {
                console.log("You have been defeated");
                slaying = false;
                dragonHit = 0;
            }
        }
    }
    else if (totalDamage < 6){
        console.log("The dragon missed you!");
    }
}

It's getting better and better.


#8

That's good! :slightly_smiling:
New things you might have a look at:

        if(totalDamage > 6) {
            totDamage = 6;
        }

What is the purpose of this? Why suppress overkill? Also why is it part of the dragons hit path as totalDamage is a player variable?

Also the dragonHit = 0 makes sense in the player branch so that you skip the dragon if you already know the game is over:

        if(playerDamage >= 6) {
            console.log("You have been defeated");
            slaying = false;
            dragonHit = 0;
        }

But in the dragon path it is rather pointless as you're already there to even see this.
Btw: You might consider to use break instead of slaying = false.

And last but not least why is your missed message dependent of the totalDamage?

 else if (totalDamage < 6){
        console.log("The dragon missed you!");
    }

If the intention is to suppress this in case of win, then it is not really necessary as you wouldn't have chosen the else path in the first place, would you?

Hope this helps, maybe you have good reasons for some of these thing.