The 1st "If" statement problem


#1


https://www.codecademy.com/courses/javascript-beginner-en-mrTNH-6VIZ9/0/4?curriculum_id=506324b3a7dffd00020bf661


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;
};
else
{
    console.log("The dragon defeated you!");
};


#2

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


#3

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 hit the dragon");
}
else{
console.log("Dragon defeat you");
}

slaying=false;

}

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

hope it will work


#4

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