Dragon Slayer! Q&A


#1

I'm on lesson 4/6 First 'if' statement.I get SyntaxError: missing after for-loop initializer. What's wrong please?! :cry:

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

var slaying = true;
while(slaying) {
  for(youHit) {
      console.log("Congratulations! The dragon is dead.You have passed your test of devotion and bravery, which is the last. Since the maiden is saved, you shall have the her to wed.");
  } else {
      console.log("Oh no! The dragon has burned you to a crisp. The maiden in distress shall surely perish. You have failed.");
  }
    slaying = false;
}

Thanks


#2

Have a look at my code below :slightly_smiling:

Spoiler

 
var slaying = true;
youHit = Math.floor(Math.random() * 2);
damageThisRound = Math.floor(Math.random()*5 + 1);
totalDamage = 0;
while(slaying === true) {
     slaying = false;
    if(youHit === 1) {
        console.log("Congrats you hit the dragon!");
    } else {
        console.log("The dragon has killed you");
    }
}


#3

Thanks @darrens12. I had written for instead of if Ooooops!


#4

Oh, nooooooooo! I'm back! I have only advanced 1 lesson, so I am on lesson 5/6 (almost done!) The second 'if' statement and hopefully my problem isn't as silly as the other one. And I get SyntaxError: expected expression, got keyword 'else'.Here's me 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("Congratulations! The dragon is dead. You have passed your test of devotion an bravery, which is your last. Since the maiden is saved, you shall have her to wed.");
  totalDamage += damageThisRound;
     if(totalDamage >= 4)
        console.log("I said the dragon is dead. So don't chop it up into pieces, it is a disgrace! STOP!")
        slaying = false;
    } else {
        youHit = Math.floor(Math.random() * 2);
    }
  } else {
      console.log("Oh no! The dragon has burned you to a crisp. The maiden in distress shall surely perish. You have failed.");
     slaying = false;
  }
}

Help! I need someone to save me from this horrible "Dragon of the Code Confusion"
Thanks


#5

Make that else if and that should work. :slightly_smiling:


#6

@benjnev,
Unfortunately changing the else to if didn't work :confused:


#7

You're missing a { after your if.


#8

Thanks @haxor789! :smile: