Whats wrong with my code?


#1

var slaying = true;
var youHit = Math.floor(Math.random() * 2);
var damageThisRound = (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 lose!");
slaying = false;
}

 else {
    console.log("You missed!");
    totalDamage += damageThisRound;
}
slaying = false;

}


#2

please post your code + error message + exercise url in the topic, not in the title of the topic. if possible use markup for your code:


#3

var slaying = true;
var youHit = Math.floor(Math.random() * 2);
var damageThisRound = (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 lose!");
      slaying = false;
  }

     else {
        console.log("You missed!");
        totalDamage += damageThisRound;
    }
    slaying = false;
}

#4

i saw my mistakes

var slaying = true;
var youHit = Math.floor(Math.random() * 2);
var damageThisRound = (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 lose!");
      slaying = false;
  }

    } else {
        console.log("You missed!");
        totalDamage += damageThisRound;
    }
    slaying = false;
}

What have i done wrong
#5

so now it is all good?


#6

yes thanks for reaching out


#7

i am confused by this code:

else {
        console.log("You missed!");
        totalDamage += damageThisRound;
    }
    slaying = false;
}

i know you passed, but it makes little sense. at this point, you already beaten the dragon or have been defeated. so you should just be able to leave it out:

var slaying = true;
var youHit = Math.floor(Math.random() * 2);
var damageThisRound = (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 lose!");
      slaying = false;
  }
}

you either keep hitting until you slay the dragon, or if you miss, the dragon gets you, and you loose.


#8

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