The second 'if' Statement confusion


#1


So, I have the code figured out but where I have put the slaying = false; is different than where the computer put it. Could anyone explain why that is? Also, the hint button has two slaying = false; statement. Any explanation on that would be helpful!


while (slaying) {
    if (youHit) {
        console.log("You hit the dragon!");
        totalDamage += damageThisRound;
        if (totalDamage >= 4) {
            console.log("You've slain the dragon!");
        } else {
            youHit = Math.floor(Math.random() * 2);
        }
    } else {
        console.log("Dragon defeated you");
    }
    slaying = false;
}


vs.



while (slaying) {
  if (youHit) {
      console.log("You hit!");
      totalDamage += damageThisRound;
          if (totalDamage >= 4) {
              console.log("You win!");
              slaying = false;
          } else {
              youHit = Math.floor(Math.random() * 2);
          }
  } else {
      console.log("You lose!");
      slaying = false;
  }
}

It doesn't really change the outcome of the code, but still I'd like to know why this happens. I've also tried putting it right below the while (slaying) { and it also worked.


#3

Hi @jinntakk, it’s hard to put it in words without some pointers, so have a look at my comments in the code:

while (slaying) {
    
    if (youHit) {                                             // ==========================
        console.log("You hit the dragon!");                   //   
        totalDamage += damageThisRound;                       //
        if (totalDamage >= 4) {                               //
            console.log("You've slain the dragon!");          //        Runs all these codes,
        } else {                                              //        before reaching slaying = false.
            youHit = Math.floor(Math.random() * 2);           //    
        }                                                     //
    } else {                                                  //
        console.log("Dragon defeated you");                   //
    }                                                         // ==========================
    
    slaying = false;         // <=== Only run slaying = false after all above code has been run, regardless the if statement is reached OR the else statement is reached.
}

Basically, your code only run through the if/else statement once, there is no loop going on. Because if you follow the logic,

while (slaying) { run everything ==>(if/else), then slaying = false }

There is no chance for the code to loop again since at the very end slaying is assigned to false, stopping the loop.

while (slaying) {
  if (youHit) {                                          // =====================================
      console.log("You hit!");                           //
      totalDamage += damageThisRound;                    //
          if (totalDamage >= 4) {                        //
              console.log("You win!");                   //
              slaying = false;                           //   if this last "IF" is reached, we want it to stop, so assigning false to slaying, stopping the loop from going on and on.
                                                         // =====================================
          } else {                                       // =====================================
              youHit = Math.floor(Math.random() * 2);    // Here is the part, where slaying is still true, when the code reaches here, it has no other code left to run after this, since slaying = true, thus, the code will repeat from the top beginning.
          }                                              // =====================================
  } else {                                               // =====================================
      console.log("You lose!");                          //
      slaying = false;                                   // this else statement is reached, you lose, therefore we want the loop to stop as well, so assigning false to slaying, loop will not continue to run after that.
  }                                                      // =====================================
}

Hope it shed some light on your situation. :slight_smile:


Try run your code many times, you will understand what I meant about your loop does not run more than once, sometimes you will easily get this printed in the console.

You hit the dragon and did 1 damage!
=> false

According to the exercise logic, you need to end your story with either Dragon defeated you or You’ve slain the dragon. Your code will only work sometimes if the generated random number is > 4.

See my code here:

Code
var slaying = true;
// A bit of new math magic to calculate the odds
// of hitting the dragon. We'll cover this soon!
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!");
    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;
  }
}

Run it a few times, you will see the difference.


#4

Hello,
would you tell me why we need the second
youHit = Math.floor(Math.random() * 2); in else statement?

I have tried to delete the code and it seems not making any differences. The game still ran well.


#5

This was really helpful thanks!


#6

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