Whats wrong with my code?


#1



Replace this line with your code. 
var slaying=true;
var youHit=Math.floor(Math.random() *2)
damageThisRound=Math.floor(Math.random()*5);
totalDamage=0;
while(slaying){
     {
        if(youHit===1){
            
          console.log("you hit the dragon");
          totalDamage+=damageThisRound;
          if(totalDamage>= 4){
              console.log("you win");
              slaying=false;
          
          
          }
          else{
              youHit=Math.floor(Math.random()*2);}
          else{
          console.log("dragon defeated you");
          
          
          
          
          
     
    slaying=false;
          }
};


#2

Hi your code should have that structture

while(condition) {#open the while loop
    if (condition) {#open the first if statement
        if (condition) { #open the second if statement

        }#close the second if statement
        else {#open the first else statement

        }#close the first else statement
    }#close the first if statement
    else { #open the second else statement

    }#close the second else statement
}#close the while loop

Hope that could help you..


#3

This should have // in front of it


#4

thankyou i got it!! but i didnt understand the program, i just have written it but can you explain whats going on here?


#5

the first if statement

if (youHit === 1) {
   console.log("you hit the dragon");
   totalDamage+=damageThisRound
}

like you can see if youHit is equale to 1 it will print "you hit the dragon" then add the damageThisRound to the totalDamage ..SO if youHit is equale to 0 it will run that..

else {
    console.log("dragon defeated you");
    slaying=false;
}

and will prin you that "dragon defeated you" then set slaying=false is here for stopping the while loop.
then in the second if statement

if (totalDamage >= 4) {
    console.log("you win");
    slaying=false;
}

then it check if the totalDamage is great or equal to 4 it will print "you win" that mean you kill the dragon and the slaying=false; ....

then if the totalDamage isn't great and equal to 4 sort again the youHit

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

HOpe that will make you undersaten it


#6

yeah thanks i understood how keywords work but i couldnt understand the meaning of those keywords