# 5/6 Why do we need the second slaying = false?

#1

I am trying to understand the code and it seems unnecessary to put slaying = false in the second if statement. I commented it out and the game still works. Can someone explain why it would be better to put that code in?

For reference, here is my code:

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("Congrats! You hit the dragon.");
totalDamage += damageThisRound;
if(totalDamage >= 4){
console.log("Player slew the dragon");
// slaying = false; <---- this is commented out and game looks like it works fine --->
} else {
youHit = Math.floor(Math.random() * 2);
}
} else {
console.log("The dragon defeated you.");
}
slaying = false;
}

#2
``````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("Congrats! You hit the dragon.");
totalDamage += damageThisRound;
if(totalDamage >= 4){
console.log("Player slew the dragon");
slaying = false; // You have killed the Dragon, Game-End
} else {
youHit = Math.floor(Math.random() * 2);
}
} else {
console.log("The dragon defeated you.");
slaying = false;  //Game ends, you are dead
}
//slaying = false;  ==>This was put in as a safety,against infinite Loop
//This is the End-of-the-while-loop, testing if =slaying= is =true=
}``````

#3

What they are trying to achieve,
that you are able to use a while-loop.

``````             point of entry
in while-loop
|
V
->  while ( condition===true )   -->EXIT--->if condition===false
|       {  // begin of while-code-block
^
|          // your code that will be executed
|          //       as long as while-condition === true
|
^<- - - } //end of while-code-block,
while-condition is checked again !!``````

They give you the code:

``````var slaying = true;

while(slaying){
console.log("I'm learning while loops!");
//Change the value of 'slaying' here!
slaying=false;   //<<== you make sure
while-condition===false
}``````

1 You assign true to the variable slaying

2 The while-statement -tests the condition-,
--which is the variable slaying with its value being true
3 Access is granted to the while-code-block
4 console.log("I'm learning while loops!"); is executed.
5 assignment of the false value to the variable slaying
6 END of while-code-block is reached,
goto 2 //The while-statement -tests the condition-, slaying

As slaying renders to false,
the while will NOT execute its body,
and will EXIT.