Declare your variables


#1

This lesson has been corrupted. You're supposed to be building the code from scratch, but the final code is in all of the examples. In every lesson after this one, you can just hit "run" and it work. But that's not the way the code was intended. The intent is to get you to recreate the code from scratch.

So the first segment should have been about setting your variables, the second was to create the while loop, etc. - in the while loop, you HAVE to set the slaying to false or else it would be an infinite loop. That's how the game ends - with a false value.

So that's why the code is correct, but the computer doesn't accept it - all of the code is there, and the end result of slaying ends up being "false" to exit the loop.

But the code that's supposed to be there at this stage, if the lesson wasn't corrupt, is just the basic setting up of the variables, in which case slaying would be true (which would set up the while loop).

The code in the "helpful response" puts the variable of true at the bottom to override the false value in the while loop for the sake of the computer giving you the ok, but it's best to declare your variables at the top.

If you want the learning experience, DELETE the code in the second example and write all of the code from scratch. The first section is just about setting up the variables, so do that, and the code clears.


#2

1. What you'll be building

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;
}
}

2. Declare your variables

var slaying = true;
var youHit = Math.floor(Math.random() * 2);
var damageThisRound = Math.floor(Math.random() * 5 + 1);
var totalDamage = 0;

3. The 'while' loop

var slaying = true;
var youHit = Math.floor(Math.random() * 2);
var damageThisRound = Math.floor(Math.random() * 5 + 1);
var totalDamage = 0;

while(slaying) {

slaying = false;

}

4. The first 'if' statement

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 === 1) {
console.log("You have hit the Dragon!")
}
else {
console.log("The Dragon has slain you!")
}
slaying = false;
}

5. The second 'if' statement

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 === 1) {
console.log("You have hit the Dragon!")
totalDamage += damageThisRound;

    if(totalDamage >= 4) {
       console.log("You have slain the Dragon!"); 
    }
}
else {
    console.log("The Dragon has slain you!")
}
slaying = false;

}

6. Well done!

var slaying = true;
var youHit = Math.floor(Math.random() * 10);
var damageThisRound = Math.floor(Math.random() * 5 + 1);
var totalDamage = 4;

while(slaying) {
if(youHit === 1) {
console.log("You have hit the Dragon!")
totalDamage += damageThisRound;

    if(totalDamage >= 4) {
       console.log("You have slain the Dragon! You have won!");
       slaying = false;
    }
    else {
       youHit = Math.floor(Math.random() * 2);
    }
}
else {
    console.log("The Dragon has slain you!")
}
slaying = false;

}


#3

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