I don't get how this principle works! help me!


#1

This is what chapter 1 shows me.
So this is what i'll be building. And i went through all of a 'Dragon slayer' chapter with very kind explanations from codeacademy.
But i just don't get how this works. so this script below is complete code to make it work.
I'll try to explain it with numbers.

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 and did " + damageThisRound + " damage!");    
    
    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;
  }
}

var slaying = true; <<<< which equals to number '1'

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

var damageThisRound = Math.floor(Math.random() * 5 + 1); <<<<<<<<

var totalDamage = 0; <<<<< totalDamage is yet 0

while (slaying). <<<<

if (youHit) <<<<

totalDamage += damageThisRound; <<<<<

if (totalDamage >= 4) {
console.log("You did it! You slew the dragon!"); <<<<>console.log("you did it! You slew the dragon!"); and go to 'slaying = false' and i will end this game. But,if the damage i've given dragon is less than 4, this will go to 'else' below that has var 'youHit'.
From here, this is what i can't understand. if the damage was less than 4 since it is in the 'while' it will do it again right? but from where? does it start again from 'if' right after 'while'?
if it is, then it could go to '0' which is false, so it could just burinate me. but I acted this coding a couple of times and it works well even when the damage was less than 3. like sometimes you hit 1 damage and you hit 2 damage you hit 4damage. and i assume it sums up each other and eventually be great than 4 or equals to 4. but i don't get how this works.

youHit = Math.floor(Math.random() * 2); <<<<<<<< And Why do i even need to write Math.floor(Math.random() * 2); again? since youHit is already Math.floor(Math.random() * 2);

I apologize with my broken English first, please help with me this!


#2

I think some of things that i've written don't work somehow.
What i want to ask is that what if that damageThisRound has given the damage less than 4?
i will go to else and it will act var 'youHit' but after that does it go back to 'if' right after 'while'?
Then, that while could work as a number '0' not '1' so it could lead to 'else' at the bottom which shows 'the dragon burinates me!'
please help me!


#3

Sry for the late response. Yes I can see that. When your initial damage (damageThisRound) is below 4 you'll always lose sooner or later. And yes your assumption is also correct you could solve this by adding up the damage e.g. something here:

if (youHit) {                          
    console.log("You hit the dragon and did " + damageThisRound + " damage!");
    totalDamage+=damageThisRound;

The general idea is that you flip a coin whether you hit the dragon or not. If you hit, you apply damage and check if you already finished it. When you finished it, congratulations -> game over. If not you need to strike again. So you flip the coin again. The reason you need the Math.floor(Math.random() * 2); again is that Math.floor(Math.random() * 2); computes a value and stays on this value. But what you want is another coin flip (true/false or in this case 1/0, same values different types) so you need to query the random function once more. Now as you picked the if branch and the if branch is done you start from the beginning of the while loop again. Check if youHit is true if yes choose the if path if no choose the else part. Let's say the second coin flip was not in your favor, then you pick the else part and it is the dragons part to attack -> game over.

Hope this makes some sense.


#4


Can you explain this?


#5

I'll try to walk through the entire code for you, and for my own sake for understanding it. Gotta get this stuff on lock-down so I really know how everything works and works together.

So, the first half of the code we are declaring our variables so that we can use them later.

var slaying = true; sets us up for when we use the while() loop later.

var youHit=Math.floor(Math.random)*2); gives us a random number from 0 to 1, but NOT including 1 (this took me a while to understand, I think the hint could be clearer). After we get our number, from 0 to 0.99 (as close as we can get to 1, but not including 1), we multiply that by 2. Now we have a number from 0 to 1.9999999998 or however close it rounds to. We take that random number and round that down. Now we only have either a 0 or a 1. So youHit can equal either a 0 or a 1 when it is called. I'm pretty sure it has no value until it is called, and then when it is called, Math.floor(Math.random)*2) is written over with the result.

var damageThisRound = Math.floor(Math.random()*5+1); starts by generating a random number, 0 to 0.99(repeating or however many decimals it rounds to). Afterward, it takes that number and multiplies it by 5. This gives us a number between 0 and 4.99(repeating or however many decimals it rounds to). We take this number and add 1 to it. We don't need to because the dragon only has 4 health, but we do this so when Math.floor() executes, we get a number between 1 and 5. Which just means we have a 2/5 chance of killing the dragon on the first turn.

var totalDamage = 0; sets the totalDamage, or the total damage inflicted on the dragon at the start of the battle or the while statement to 0.

Now we start the battle. It starts automatically without prompt. You are also not given the choice to fight the dragon again. You'd have to execute the program again to fight it again.

Now then, while (slaying) { tells us to start doing the following actions while true. Keep in mind, slaying is a variable that holds the value of true that we declared it to be earlier. So when see while (slaying) {, the computer sees while (true) { and will keep looping until while is told false.

The next statement is the bulk of the battle. Everything we've set up earlier takes place now.

if (youHit) { starts the if statement block. This is checking if youHit is either 0 or 1. If it is a 0, it is evaluated to false. If it is a 1, it is evaluated to true. It will execute the following code if the value is true.

console.log("Congratulations ! You hit the dragon!" + damageThisRound);
This code logs the text Congratulations! You hit the dragon! to the console window, followed (which is what the + does) by the value of damageThisRound. It then closes the console.log( with a ) and ends the line with the ;.

totalDamage += damageThisRound; comes next, right after the console.log(); line.
The += is a shorthand way of writing totalDamage = totalDamage + damageThisRound. It adds up the total damage and the damage you just did, and overwrites that to the total damage variable.

After this is done, it's still executing the code that for when youHit was true, or 1, but we want to check if the dragon is dead yet or not. We do this now because we just hit the dragon and we want to check if that hit we just made was the killing blow.
A second nested if statement is started, if (totalDamage >= 4 { which checks if the totalDamage is now greater than or equal to 4, AFTER we added the damage from that hit above. This could be any number, but we set this to 4, representing the health of the dragon. If you want the dragon to have 5 health, change this to 5. Perhaps an better way to understand this would be to create another variable at the top of the code.
var dragonHealth = 5
if (totalDamage >= dragonHealth) {

Then we can change how much health the dragon has by changing the variable for it.

Now, if totalDamage is greater than 4, it is evaluated to true, and the following code executes.
console.log("You slew the dragon!"); , which just logs that string to the console. Then, still in this second if statement, it also executes slaying = false;, which changes the value of slaying to false. After this is executed, it hops out of the second for loop and back into the first for loop. Since there is nothing left to do in the first for loop, it hops back to the while loop. Now while is evaluated again, this time to false because we just changed slaying = false;.

Now then, if totalDamage was NOT greater than or equal to 4, the code
}
else {
youHit = Math.floor(Math.random()*2);
}
is executed. It overwrites the previous value of 1 that was generated when we first declared it to a new randomly generated value of either 0 or 1. Then we end the else statement for the second nested if statement. Since that was the end of the second nested if statement, we hop back to the first nested if statement. Since there is also nothing else to do for the first nested if statement, we hop back to the while statement. Only through this route we did NOT change slaying to false. It is still true. So the while statement will then execute again.

If youHit (line 6) evaluates to 0, which is to say, false, we go to the else statement (line 17) below,
}
else {
console.log("The dragon as defeated you!");
slaying = false;

This makes it so that if you miss even ONCE (when youHit evaluates to false) you go right to the else statement on line 17 that declares the dragon the victor.

It then closes the first nested if statement, and the while statement is run again. However, we just changed slaying to false because the dragon killed you and the game should now be ended. while (slaying) evaluates to false this last and final time, so false is logged there at the end of the console.

There is now no further code to execute.

I really hope this helps you and others understand. It helped me understand just by writing it out.