Set slaying to true


#1

I have it at the top -- var slaying = true. When I change at the bottom after the dragon is slain, Firefox gives me this message before I can continue:

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("You hit the dragon and did " + damageThisRound + " damage!");
totalDamage += damageThisRound;

    if (totalDamage >= 4) {
  console.log("You did it! You have slain the dragon!");
  slaying = false;
    }
    }
    else {
console.log("yer dead.");
slaying = true;
    }

}


#2

@lodum,

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("You hit the dragon and did " + 
                     damageThisRound + " damage!");
        totalDamage += damageThisRound;
        if (totalDamage >= 4) {
            console.log("You did it! You have slain the dragon!");
            slaying = false;
        }
        else {
            youHit = Math.floor(Math.random() * 2);
            slaying = true;
        }
    }
    else {
       console.log("yer dead.");
       slaying = false;
    }
}

#3

When you're dead, slaying is over so you should set this to false


#4

I have fought with this for days and still can't get it. I refresh after every change, to no avail. I'm baffled, where is the error?

Any help is gratefully appreciated!

Most recent code:


#5

@lodum,
The strange thing is
this "Oops-message" is only generated in the section "Declare your variables"
which is the 2nd Section.....

I would suggest

  • you go back to this 2nd section
  • click-on-the-Reset button
  • and do as the Instruction is asking you....

If you get the POPUP as you have displayed in your prior Post
click on =continue=.....


#6

I clicked Reset Code; no Instructions appeared so I re-entered the Code (also trying to better organize my brackets); I am getting a new message, but am no closer to a solution, and don't see Continue as an option . . . ideas?


#7

@lodum,
Well you have to be more secure in writing your code....
not-defining-totalDamage,
misplacing closing curly-brackets....

Please give the code you are using as ASCII......


#8

Thank you for the compliment asking me to submit my code in ASCII! I am a beginner with coding -- took your HTML5 and CSS then launched into Java Script. I have no idea how to do anything in ASCII.

I defined totalDamage; I have 5 open brackets and 5 closing brackets. Am I openinig or closinig in the wrong place? I get this message on the preview panel: SyntaxError: expected expression, got keyword 'else'. what does that mean?

Thank you for taking the time with a rank beginner.


#9

Guess it would help if I showed you the current iteration of 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 ("You hit the dragon and did " + damageThisRound + " damage!");
        totalDamage +=damageThisRound;
        
         if (totalDamage >= 4)
         { console.log("You have slain the dragon!");
         slaying = false
         
    
            else
            { youHit = Math.floor(Math.random() * 2);
            slaying = true;
            }
            }
    }   
            else
            { console.log ("Yer dead.");
            slaying = false;
            }
}

#10

@lodum,

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) { 
    totalDamage +=damageThisRound;
    console.log ("You hit the dragon and did " + 
                 damageThisRound + " damage!" +
                 " Total-damge: " + totalDamage);
    if (totalDamage >= 4) {
        console.log("You have slain the dragon!");
        slaying = false;
    }  //forgot this one
    else { 
        youHit = Math.floor(Math.random() * 2);
        slaying = true;
    }
//    }
  }   
  else { 
        console.log ("Yer dead.");
        slaying = false;
  }

}
nice to know

You could test in labs repl.it
choose Javascript
You will get a split-screen
In the left-hand-side you paste-in your code
click on the Run button.
The right-hand-side is the console

references

www.crockford.com --> for most respected guide-line in Javascript
www.crockford.com
http://javascript.crockford.com/code.html <== Javascript code-convention
http://javascript.crockford.com/survey.html


#11

That's probably what he meant by providing the code as ASCII. ASCII is the "American Standard Code for Information Interchange", basically one of the first or even the first commonly used "computer alphabets" (english alphabet+numbers+special signs like !'%&/>< aso + some command signs). So basically the alphabet which you are using for any coding as many languages aren't as advanced to support Unicode characters for more then strings.

Guess the intention was that if we can copy you're code we can run it ourself instead of looking at an image.


#12

Thank you for the explanation --- but I have no training in ASACII.


#13

Can you look at an image and tell me what is incorrect? So far I seem to be stymied by setting "slaying to true"; HOWEVER, of the 4 slayings, 2 are set to true -- line 1 and line 17, 2 are set to false -- line 13 and line 22. What is the error? See snip below:


#14

No need to that is the alphabet you're currently using :slight_smile: just meant that you should post copyable code.

As far as I can see the code is fine although the 2nd slaying =true seems to be redundant as it should already, or still, be true. What exercise are you working on as @leonhard.wettengmx.n mentioned that this is an error that normally only occurs on exercise 2 so maybe you're a little ahead of the game. But that is just a guess. So could you give some more information?


#15

I am on Dragon Slayer Declare your variables. I have tried several iterations of code, nothing works. See current code below. I have been struggling with this for 2 weeks and can’t find a solution.

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) { 
    totalDamage+=damageThisRound;
    console.log("You hit the dragon and did " +  damageThisRound + " damage!"); 

    if (totalDamage >= 4) {
             console.log("You have slain the dragon!");
               slaying =false;

   } else { 

        youHit =
Math.floor(Math.random() * 2);

/*removed slaying =true here as you suggested.  I still get the same message: “Oops, try
again. It looks like you didn't set slaying to true.*/

    }

  }   

  else { 

        console.log("Yer dead.");

        slaying =false;

  }

}

#16

Ok that explains a lot. As said you're probably ahead of the game as it just expects this part:

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

But after you successfully ran your code the value of slaying is obviously false because you left the loop. So save this code just reduce it to the necessary part and go through the other exercises with this code as it is almost a finished game, isn't it.


#17

Oh for heaven's sake!! I just spent two weeks feeling like a numbskull!! Thank you for your help, I'll go back and only put in the top part.