Please help me to know that how I get a error about "syntax." THX!:)


#1


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

Oops, try again. There was a problem with your syntax.


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

var killdragon = function()
    {
        while(slaying)
            {
                console.log("Kill the dragon!");
                slaying = false;
                if(youHit)
                    {
                        console.log("You hit!");
                        totoalDamage += damageThisRound;
                        if(totalDamage >= 4)
                            {
                                console.log("You win!");
                                slaying = false;
                            }
                        else
                            {
                                      youHit = Math.floor(Math.random() * 2);
                            }
                        else
                            {
                                     console.log("the dragon defeated you!");
                                     slaying = false;
                            }
                    }
            }
    
    }
killdragon();


#2

Hi it's because you close the first if statement after the else statement...

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

var killdragon = function()
    {
    while(slaying)
        {
        console.log("Kill the dragon!");
        slaying = false;
        if(youHit)
        {
            console.log("You hit!");
            totalDamage += damageThisRound;
            if(totalDamage >= 4)
            {
                console.log("You win!");
                slaying = false;
            }
            else
            {
                youHit = Math.floor(Math.random() * 2);
            }
        } <-- #add this one for closing the first if statement
        else
        {
            console.log("the dragon defeated you!");
            slaying = false;
        }
    }
   } < -- #remove this one 
}
killdragon();

Read the comments


#3

Thank you so much!!! You made me suddenly seeing the light! :slight_smile:


#4

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