Understanding Dragon Slayer code


#1

So I finished the Dragon Slayer course, but am curious where and what some of the numbers mean:
Please see bold below:

//*declaring the variables*//
var slaying = true;
var youHit = Math.floor(Math.random() * 2); **I dont understand the "*2" and what it means?**
var damageThisRound = Math.floor(Math.random()*5+1);     **I dont understand the *5+1 part**
var totalDamage = 0;

//*while Loop area*//
var slaying = true;
while(slaying) {     /*checks variable to see if its true*/
    if (youHit) {           /*if/else statement *//*checks the value of youHit */
        console.log("You hit the dragon!"); 
        totalDamage += damageThisRound;
        if (totalDamage >= 4) {       **I dont understand the ">=4" part. why 4?**
            console.log("You hit the dragon! Keep it up!");
            slaying = false;
        } else { 
            youHit = Math.floor(Math.random() * 2);
        }    
    } else {                                
        console.log("you missed him!...You just died!"); 
    }    
        slaying = false; /*checks variable to see if its true*/
    }

#2

As Math.random creates numbers between 0 (included) and 1 (not included) using a Math.floor on it make it pretty pointless as it would be always 0 as even 0.99999999999999999999999 is rounded down to the next integer -> 0.

So by multiplying it with a number we increase the possible outcomes. e.g. 2 would mean that anything that is bigger than 1/2 (or 0.5 as you like) would be 1 and anything lower would be 0. so Math.floor(Math.random() * x) creates x distinct intervals. Now why 2? because we want to emulate a coin flip a true or false option. It probably would have been better to use

if (youHit ===1)

here but as the value 1 behaves like true and 0 as false it still works. For 5 and 4 later it is almost arbitrary you just want a max damage and some sort of HP for the dragon. One last words about the +1. As multiplying Math.random() changes the range of numbers adding numbers changes the start and end value so the +1 probably should avoid that you loose because you're max damage is 0. Because this would mean hitting forever without having a chance to win.


#3

Math.random() return's a number Value in the range of 0 and 0.9999
If you multiple this returned number Value with 2
that would leave you a range
0...0.49 *2 ==> 0..0.99
0.50...0.999 *2 ==> 1....1,999

If you then do a Math.floor() that would lead to
0...0.49 *2 ==> 0..0.99 =>Math.floor() ==> 0
0.50...0.999 *2 ==> 1....1,999 => Math.floor() ==> 1

Thus the youHit variable has an associated _number Value of 0 OR 1
and you then use the Javascript truthy or falsy facility
https://developer.mozilla.org/en-US/docs/Glossary/Truthy
https://developer.mozilla.org/en-US/docs/Glossary/Falsy


#4
  1. What does the Math.floor(Math.random()*5+1)? Specifically the 5+1?

  2. I dont get the -> if (totalDamage >= 4) in the coding. What does the 4 half to do with the outcome? and why 4?


#5

What does the Math.floor(Math.random()*5+1)? Specifically the 5+1?

It's not 5+1 its Math.random()*5 and the result +1. Remember math in school * has a higher priority than +. If it feels more intuitive to you, you can even write it this way:

Math.floor(Math.random()*5)+1

I dont get the -> if (totalDamage >= 4) in the coding. What does the 4 half to do with the outcome? and why 4?

The numbers 4 and 5 are completely arbitrary they are just an estimate for the HP of the dragon and the attackDamage of your hero they could be anything else. But for a game it would make sense that they are in the same range 10000 attackDamage to 1 HP would be boring and 1 attack damage to 100000 HP would be frustrating. And 0 attackDamage to anything else in HP would be impossible. So they picked 4 and 5 but again they are completely arbitrary.

Could you be a bit more precise of what you don't understand? Because many of what I would want to say is already written in my first post.


#6

@bobsigny,
Math.random() =>Return-value=> 0 ... 0.9999
Math.random()*5 =>Return-Value=> 0....4.99995
Math.random()*5+1=>Return-Value=> 1....5.99995
Math.floor(Math.random()*5+1)=>Return-Value=> 1....5

So the variable damageThisRound has the slightest change to be greater-equal 4,
with this damageThisRound Value you are going into the =Battle=,
and if that is the case you will only have to HIT the Dragon once....

So now calculating the your chance of killing the Dragon in 1 strike would be
The chance of youHit being 1 is 50%
The chance of damageThisRound being 4 or 5 is 2/5 or 40%
Thus your actual chance of killing the Dragon in the 1st Round is 50% * 40% is 20%

If you would use the condition (totalDamage >= 5)
your chance killing the Dragon in the 1st strike would be 50%*20% is 10%

If you would use the condition (totalDamage >= 3)
your chance killing the Dragon in the 1st strike would be 50%*60% is 30%