SyntaxError


#1

Help me Thanks in advance :wink:

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 = 1)
    {
        console.log("Amigo, you hit the dragon ");
        totalDamage + = damageThisRound;
    }
        if(totalDamage >= 4)
        {
            console.log("Stupendous, you slew the dragon");
            slaying = false;
        }
        else
        {
            youHit = Math.floor(Math.random() * 2);
        }
    else
    {
        console.log("Dragron ripped your dear");
    }
    slaying  = false;
}

Error :-
SyntaxError: Unexpected token =


#2

@pysurfer02955,

I believe you'll be able to find your problem on this line :smiley:

if (youHit = 1)
           ^

#3

if you mean "===" instead of "=" i have tried it still same error


#4

Alright I found it!

 if (youHit = 1)
    {
        console.log("Amigo, you hit the dragon ");
        totalDamage + = damageThisRound;
    } //Closing bracket here closes this if statement.
        if(totalDamage >= 4)
        {
            console.log("Stupendous, you slew the dragon");
            slaying = false;
        }
        else
        {
            youHit = Math.floor(Math.random() * 2);
        } 
    else //This else statement is part still a part of the other else statement.
    {
        console.log("Dragron ripped your dear");
    }

Either you were trying to make a nested if statement, and then closed the bracket too quick, or you were trying to make the first "else" statement an "else-if" statement. Focus on these areas and determine what you're trying to do with this code and that should give you the answer!

I hope this helps!


#5

hey i found it
1. 'if' condition should cover the other 'if' condition as you have stated.
2. totalDamage + = damageThisRound;
There should be no space between '+' and ' ='