6/6 Code work and it says congratulations but I have a problem


#1

Okay my code is below. The code works and says congratulations move onto the next course but if I hit the dragon and don't kill it it just returns 'you hit the dragon' 'false'. How do I make it so it keeps hitting until totalDamage is >= 4

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("You hit the dragon!");
    totalDamage += damageThisRound;
    if(totalDamage >= 4){
        console.log("You killed the dragon!");
        slaying = false;
    } else {
        youHit = Math.floor(Math.random() * 2);
    }
} else {
    console.log("The dragon defeated you!");
}
slaying = false;
}

#2

There seems to be a few problems with the code, and they are the following:
1. On line 7 - if (youHit) {
youHit should be set to 1, which the computer understands as true, so then the computer would proceed to run the while loop. In the code, you are calling the global variable which assigns the condtion either 1 or 0 and we only want the computer to say, hey you actually hit because youHit is true, so this is the result.
2. And this one is more of a comment than an actual issue, it would be helpful if you spaced out your code.


#3

On line 19, you need to move the slaying = false; into your if/else statement. Currently, it is at the end of your while statement, which terminates the loop and stops it from repeating. Had the same problem, took a bit to figure out.

TL:DR Swap lines 18 and 19.


#4

i have an issue if you dont mind helping out.
it works if i kill it right away or if i get killed right away. but when i hit it and dont kill it. it ends
example:


you hurt the dragon!
you delt 2 damage!
the dragon has taken 2 damage so far!
false


i tried doing what you said, "you need to move the slaying = false; into your if/else statement. Currently, it is at the end of your while statement, which terminates the loop and stops it from repeating.'
but when i do that it become eternal loop and crashes.

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){
if(youHit){
console.log('you hurt the dragon! ');
totalDamage += damageThisRound;
console.log('you delt ' + damageThisRound+ ' damage!');
console.log('the dragon has taken ' + totalDamage + ' damage so far!');
if(totalDamage >= 4){
console.log('you did it, you killed the mystical beast.');
slayer = false
} else {
youHit = Math.floor(Math.random() * 2);
}

} else {
    console.log('you missed... the dragon has brutally killed you and your entire family. ');
}

slaying = false;

}



#5

Hey Mathew,

I'll try make this a clear as I can. Here goes. This is your code, I hope:

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('you hurt the dragon! ');
         totalDamage += damageThisRound;
         console.log('you delt ' + damageThisRound+ ' damage!');
         console.log('the dragon has taken ' + totalDamage + ' damage so far!');
               if(totalDamage >= 4){
                      console.log('you did it, you killed the mystical beast.');
                      slayer = false
               } else {
                      youHit = Math.floor(Math.random() * 2);
               }

    } else {
           console.log('you missed... the dragon has brutally killed you and your entire family. ');
    }
slaying = false;
}

So there are two errors with this code that I see. Firstly, in your second if statement, you need to close the line:

slaying = false with a ;

This might be causing your crash.

Secondly, like the OP (original poster), you need to move the second:

slaying = false; into this else statement:

Currently, if you look at the way I've laid out your code, the second slaying = false; is nested in your While statement instead of the If statement.

What this means is that the While loop will terminate when you do damage and don't kill the dragon because you have set slaying=false; within the while loop and not the if statement. This results in your While loop only running once.

Hope this helps. If I need to be more clear or with these changes it doesn't work/keeps crashing, let me know. Good luck!


#6

auzfather, thanks for the help. that made it work great.


#7

I have a doubt in this "false" is also printed on the console , when the false is not in console.log();
Please help me in this!
else {
console.log("The dragon burninates you! You're toast.");
slaying = false;
}