The 1st "If" statement problem


The error message that I get is Unexpected Token error.

I have looked at other people's code and gone through several changes but it still doesn't work. I'm not sure where to go from here.

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

while (slaying){
    if(youHit === 1) {
    console.log("You hit the dragon " + damageThisRound + "Congrats!");
    slaying = false;
    console.log("The dragon defeated you!");


Check the placement of your "else" block. Did you mean to put it outside of the while loop?


var slaying= true;
var youHit= Math.floor(Math.random()*2);
var damageThisRound=Math.floor(Math.random()*5 +1);
var totalDamage=0;
if(youHit ===1){
console.log("you hit the dragon");
console.log("Dragon defeat you");



see this code and compare it with your you have done many mistakes
1.use Math.floor
2.remove semicolon after curly bracket curly bracket is extra befor else statement ,remove it and if, else should be within while loop terminate while loop use slaying=false before the end of while loop

hope it will work


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