Dragon Slaying First if Statement Wrong Hint?!


#1

Can anyone tell me what's wrong with this? I tried for a long time and it still didn't work and I checked it and it was exactly the same as the hint then I just copied and pasted the answer and yet it STILL DIDN'T WORK! Please tell me what's wrong and why the answer doesn't work!
var slaying = false;
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!");
} else {
console.log("You missed!");
}
slaying = false;
}

This is the error: "Make sure you log a message to the console to let the player knows if (s)he won or lost!" But I'm pretty sure I did!


#2

@nintenjoe10,

Have a look at how a WHILE-loop works....

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 understand = true;

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

1 You assign true to the variable understand

2 The while-statement -tests the condition-,
--which is the variable understand 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 understand
6 END of while-code-block is reached,
goto 2 //The while-statement -tests the condition-, understand

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

Reference::

google search
== the Book ==
javascript while site:developer.mozilla.org
https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/while

== discussions / opinions ==
javascript while vs do while site:stackoverflow.com
http://stackoverflow.com/questions/5599027/the-do-while-statement

== guidance ==
www.crockford.com
http://javascript.crockford.com/code.html
http://javascript.crockford.com/survey.html
https://developer.mozilla.org/en-US/docs/Web/JavaScript/A_re-introduction_to_JavaScript


#3

Ooooooooooh! Oops, I didn't see that :blush:


#4

your variable "slaying" should declare to "true" and NOT "false" Try !


#5

No, it says many times that it must be false. Plus I'm pretty sure that's wrong because I figured it out (thanks to leonhard) and that didn't have anything to do with it. Thanks anyway, though!


#6

Curios how you made variable slaying to false and be able to execute while loop. Can you post your code how you code to sole the problem? I coded mine with declaring variable slaying to true and pass the code !<img src="//codecademy-discourse.s3.amazonaws.com/original/4X/f/4/8/f48fd2fa1be629bead5405e2cec2417fddf78aa3.png" width="690" height="431">