Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In this game, youll battle a dragon. It will take 4 hits to slay the dragon, and if you miss even one hit, the dragon

In this game, youll battle a dragon. It will take 4 hits to slay the dragon, and if you miss even one hit, the dragon will defeat you!

1) All right! Let's start by declaring the variables we'll be using. We'll need:

a variable to check if we're still slaying

a variable to check if we hit the dragon

a variable to keep track of how much damage we've dealt the dragon this round

a variable to keep track of total damage

Declare and set the following variables:

slaying equal to true

youHit to Math.floor(Math.random() * 2). This sets youHit to a random number that's either 0 (which JavaScript reads as false) or 1 (which JavaScript reads as true).

damageThisRound toMath.floor(Math.random()*5 + 1). This sets damageThisRound to a random number that's between 1 and 5 (up to and including 5).

totalDamage to 0

2) Awesome! Now let's add in our whileloop. We want to run the whole game as long as we're trying to kill the dragonthat is, while slaying istrue.

When checking variables like slayingthat are set to true, you don't need to write something like:

while(slaying === true)

You can just write

while(slaying)

It also helps to give your variables names that make the code look more like regular English. while(slaying) {/*Do this*/ } is easy to remember because it's so close to everyday speech!

3) Great! Now we want to add a couple of branches to our program so it can handle different outcomes. You know what this means: if and else!

Inside your while loop, create anif/else statement that checks the value of youHit. If it's 1 (true), it should log a congratulatory message to the console, saying that you hit the dragon. If it's 0 (false), it should log a message to the console saying that the dragon defeated you.

Either way, slaying should be set tofalse, since either you beat the dragon (and the slaying's over) or the dragon beat you!

4) Inside your while loop, create anif/else statement that checks the value of youHit. If it's 1 (true), it should log a congratulatory message to the console, saying that you hit the dragon. If it's 0 (false), it should log a message to the console saying that the dragon defeated you.

Either way, slaying should be set tofalse, since either you beat the dragon (and the slaying's over) or the dragon beat you!

Go ahead and set totalDamage tototalDamage plus damageThisRound.

Then, inside your first if statement, create a second if statement that checks to see if totalDamage is greater than or equal to 4. If so, it should log to the console that the player slew the dragon and set slaying equal tofalse (since the dragon's dead, the slaying is over).

If totalDamage isn't greater than or equal to 4, youHit should be assigned a new random 1 or 0. (This is as easy as setting youHit to the same expression you used when you first declared it.)

5) Awesome! Now let's add in our whileloop. We want to run the whole game as long as we're trying to kill the dragonthat is, while slaying istrue.

When checking variables like slayingthat are set to true, you don't need to write something like:

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions

Question

Describe how language reflects, builds on, and determines context?

Answered: 1 week ago