Dragon Slayer > 5. The second 'if' stament


#1

var slaying = true;

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

while(slaying) {

if (youHit = true) {
console.log("You beat the ■■■■ out of the dragon! ■■■■ yeah!");
totalDamage += totalDamage + damageThisRound;
if (totalDamage >= 4) {
console.log("You killed the mythical beast.")
slaying = false;
else {
youHit = Math.floor(Math.random() * 2);
console.log("You lose.");
}
}
else {
console.log("The dragon wins!");
slaying = false;
}

I'm getting: SyntaxError: Unexpected token else
And anything else wrong with my codes?


#2

Hey @dyan_aguirre03gmail_ Your getting the syntax error else since you cant put two else statements together.

Here is the part you had the problem

Just change on of them to else if and you should be fine! If your not please reply saying so!


#3

where is says

totalDamage += totalDamage + damageThisRound;

this is what you are saying in the code

totalDamage = totalDamage + totalDamage + damageThisRound

try changing it to.

totalDamage += damageThisRound;

#4

var slaying = true;

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

while(slaying) {

if (youHit = true) {
console.log("You beat the ■■■■ out of the dragon! ■■■■ yeah!");
totalDamage += damageThisRound;
if (totalDamage >= 4) {
console.log("You killed the mythical beast.")
slaying = false;
}
else {
youHit = Math.floor(Math.random() * 2);
console.log("You lose.");
}
} else {
console.log("The dragon wins!");
slaying = false;
}
}

I finally got it. I closed the 2 first ifs then another 2 else seperately. Thank you so much for all your help. (: