Second If statement Syntax Error


#1


where have i made error? I could not identify the error.

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=true)
    {
        console.log("you just hit the dragon.");
        totalDamage+=DamageThisRound;
        if(totalDamage>=4)
        {
            console.log("You Win!");
            slaying=false;
        }
        else
        {
            youHit=Math.floor(Math.random()*2);
        }
        else{
            console.log("you lose!");
            slaying=false;
        }
    
    }
    else(youHit=false)
    {
        console.log("the dragon defeated you.");
    }
    slaying=false;
}


#2

The problem is here:

        if(youHit=true)

The "=" is an assignment operator in JavaScript. So the line above is assigning the value "true" to the variable "youHit", when what you want to do is test whether the value of youHit is true. To do this, you need to use the equality comparison operator, which is "===".


#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)
{

    console.log("you just hit the dragon.");
    totalDamage += damageThisRound;
    if(totalDamage>=4)
    {
        console.log("You Win!");
        slaying=false;
    }
    else
    {
        youHit=Math.floor(Math.random()*2);
    }
}    
else
      {
     console.log("you lose! -----ACtualy means dragon win");
       }




   
     slaying=false;

}

see you have done silly mistakes
1.damageThisRound has capital D

2.when you assign true for youHit it will always be true but it should not be that's why we are using Math.random and also if you continue to use true then you will never lose you either hit dragon or win

3.the last else is extra becuse if you not hit it will goes to you lose which means dragon defeat you so delete it

4.slaying=false which is just below "you lose" , should be out of else loop and should be pasted just before while loop ends.

hmmmm That's it


#4

@pypro05664

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)
{

console.log("you just hit the dragon.");
totalDamage += damageThisRound;
if(totalDamage>=4)
{
    console.log("You Win!");
    slaying=false;
}
else
{
    youHit=Math.floor(Math.random()*2);
}

}
else
{
console.log("you lose! -----ACtualy means dragon win");
}

 slaying=false;

}

see you have done silly mistakes
1.damageThisRound has capital D

2.when you assign true for youHit it will always be true but it should not be that's why we are using Math.random and also if you continue to use true then you will never lose you either hit dragon or win

3.the last else is extra becuse if you not hit it will goes to you lose which means dragon defeat you so delete it

4.slaying=false which is just below "you lose" , should be out of else loop and should be pasted just before while loop ends.

hmmmm That's it


#5

hey thank u so much for helping me out but even after making changes i could not run it and it seem i am stuck
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===true)
{
console.log("you just hit the dragon.");
totalDamage+=damageThisRound;
if(totalDamage>=4)
{
console.log("You Win!");
slaying=false;
}
else
{
youHit=Math.floor(Math.random()*2);
}
else{
console.log("you lose!");
}

}

slaying=false;

}


#6

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