Syntax error: Unexpected token { . What's wrong with my code? It looks the same with the code in the Hint but I'm still getting that error


#1


The second 'if' statement

Syntax error: Unexpected token { .


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

while(slaying)
{
    if(youHit)
    {
    console.log("You hit the dragon");
    totalDamage += damageThisRound;
    if(totaDamage>=4)
    {
        console.log( " You did this to the dragon");
        slaying=false;
    }
    else if
 {   
youHit=Math.floor(Math.random()*2) ;   
}

    
    else
    {
        console.log("The dragon defeat you");
        slaying = false;
    }
 }
        }

Continuing the discussion from Unexpected token {:


#2

You have problems on the positions of your curly brackets. hope this helps:

while(){  //open curly bracket of your WHILE

    if(){ //open curly bracket of your FIRST IF
        if(){ //open curly bracket of your IF inside your FIRST IF
            } //close curly bracket of your IF inside your FIRST IF
        else{  //open curly bracket of your ELSE inside your FIRST IF
            } //close curly bracket of your ELSE inside your FIRST IF
        } //close curly bracket of your FIRST IF

    else{ //open curly bracket of your ELSE
        } //close curly bracket of your ELSE

    } //close curly bracket of your WHILE

And one more thing, on this line:

You must declare the variable damageThisRound above instead of youDamage;


#3

thank you so much @gracie017


#4

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