5/6 unexpected token else..please help


#1

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("congrats you slayed the dragon");
    totalDamage += damageThisRound;

if(totalDamage >= 4){
    console.log("congrats you slayed the dragon");
    slaying = false;
}

}else{
      youHit = Math.floor(Math.random() * 2);

}else {
    console.log("the dragon slayed you");

}
slaying= false;
}

I just cant figure out why I am getting this unexpected token error...please help


#2

}else{
youHit = Math.floor(Math.random() * 2);
}

you have one } to much

it should just be

else {
youHit = Math.floor(Math.random() * 2);
}

I too had that mistake :smile:


#3

I deleted the extra "}" and still get the same syntax error....its driving me crazy

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("congrats you slayed the dragon");
    totalDamage += damageThisRound;

if(totalDamage >= 4){
    console.log("congrats you slayed the dragon");
    slaying = false;
}

else{
      youHit = Math.floor(Math.random() * 2);

}else {
    console.log("the dragon slayed you");

}
slaying= false;
}


#4

never mind..I figured it out..thank you "}" was placed wrong...here is correct 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){
    console.log("congrats you slayed the dragon");
    totalDamage += damageThisRound;

if(totalDamage >= 4){
    console.log("congrats you slayed the dragon");
    slaying = false;


} else {
      youHit = Math.floor(Math.random() * 2);
}

} else {
    console.log("the dragon slayed you");

}
slaying= false;
}


#5

i dont know about you guys but i posted the code in and removed the else and brackets above console.log and it worked fine