5. The second "if" statement - what's wrong with my syntax?


#1

doesn't work....

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 hit!");
totalDamage += damageThisRound;
if (totalDamage >= 4) {
console.log("You win!");
slaying = false;
}
else {
youHit = Math.floor(Math.random() * 2);
}
else {
console.log("You missed!");
slaying = false;
}
}
}


#2

You have some brackets in the wrong area. You have an extra one at the end, and you need to have one before the last else statement.


#3

thank you! ^_^
it's working :smile:


#4

what is the codes? can i see?


#5

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 hit!");
totalDamage += damageThisRound;
if (totalDamage >= 4) {
console.log("You win!");
slaying = false;
}
}
else {
youHit = Math.floor(Math.random() * 2);
}
else {
console.log("You missed!");
slaying = false;
}
}


#6

its already running?


#7

try this

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 hit!");
totalDamage += damageThisRound;
if (totalDamage >= 4) {
console.log("You win!");
slaying = false;
}

else {
youHit = Math.floor(Math.random() * 2);
}
}
else {
console.log("You missed!");
slaying = false;
}
}