8. Dragon Slayer - Did I finish the code correct?


#1


I just finished the code, but I'm not sure if everything is correct.

Here is why:
1st:(I'm not sure If thats correct but:) Weren't you supposed to hit again if you miss the first time? But somehow if I get 0 or 1 dmgoutput it doesnt hit again.

2nd: (This may contradict with what i claimed above but maybe someone will understand what my problem is):
Once, after I clicked about a dozen times on "Save & Submit Code", i managed to hit 4 times (with each 1 dmg output) and slayed the dragon. Maybe u'll get now what my concern is. Why does it sometimes hit only once but sometimes twice or even like once 4 times? But i never get to hit (e.g). 1st. hit =1 dmg ---> 2nd. hit =3dmg (thus the dragon would be slayed) or 1st. hit = 0dmg and 2nd hit (e.g) = 2dmg (thus i still would be defeated).

Somehow im not satisfied with the random algorithm of this code, because i get defeated way too often after 1st hit, or i slay the Dragon way too often with 1st hit. But i also dont know how to change the code to get a better random algorithm out of this game.

could someone help me with my issue? I dont know....maybe the code is correct, but my console output is not correct (maybe i'm missing some additional console.logs)?

I would suggest you to try my code a couple of times yourself, then u can see the different console outputs i'm getting

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 == 1 ){
console.log("You hit the Dragon and did "+ damageThisRound + " damage")
totalDamage += damageThisRound;
} else {
console.log("The Dragon defeated you");
slaying = false;
}
if (totalDamage >= 4){
console.log("You slew the mighty Dragon")
slaying = false;
}else {
youHit = Math.floor(Math.random() *2)
}
};

`


#2

No,look at your code

while(slaying){
if (youHit == 1 ){
console.log("You hit the Dragon and did "+ damageThisRound + " damage")
totalDamage += damageThisRound;
} else {
console.log("The Dragon defeated you"); 
slaying = false;
}

so here if youHit==0 then it'll run else statement and here slaying=false
so while loop will be stop.

again see your code

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

here the random number will be either 0 or 1
so you cant hit twice at a time

At first you should know how Math.random works and Math.floor too .

you can do your if else statement inside if ......like

while (slaying) {
  if (youHit) {
console.log("You hit the dragon and did " + damageThisRound+" damage!" );
    totalDamage += damageThisRound;
    
       if (totalDamage >= 4) {
        console.log("You did it! You slew the dragon!");
        slaying = false;
          }
       else {
      youHit = Math.floor(Math.random() * 2);
         }
     } 
   else {
    console.log("The dragon burninates you! You're toast.");
    slaying = false;
  }
}

hope you've understand
please feel free to ask :slight_smile:


#3

ok, thanks for the answers. helped alot. :+1:


#4

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.