The Second IF statement


Hey guys, I've just finished the second IF statement and its displaying my code without any error messages but when I run it the only outcome is that I seem to lose every time, it is just because of chance or have I missed something?


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

    console.log("Yassss you slayyyy!");

if (youHit === true)  {
    console.log("Yas you slayed da dragon. OOohh he needs some milk!");
    totalDamage += damageThisRound;
    slaying = false;
        if(totalDamage >= 4) {
            console.log("SLEW DEMMM. Dragon KO'd");
            slaying = false;
            else {
                youHit = Math.floor(Math.random()*2);
    console.log("Dragon whooped yo ass bruh. Damn Daniel :(");


It works fine, though you forgot to use var keyword while creating variables!


Ahh right ok, thanks for that @greentreemee. The result kept coming back that i'd lost every time, just wanted to check. Var's added now!


Does it solve the problem?


Hmmm no its still only producing one result. I even increased the youHit var and decreased the totalDamage to give a better probability but still same result :confused:


var is lowercased not Var.. tell me what you get by doing that..ok


Yeah i'm still getting the same result. I'm not sure if its the order of my statements, it works but its annoying that I don't get both results. I don't think anybody would want to play this game


I have the same problem, some time i get like 4 results put if I try to rerun the code it stucks in looping and the browser frozen
here is my code

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

if (youHit){
console.log("you hit!");
totalDamage += damageThisRoound;
console.log("you win!");
}else {
console.log("dragon defeated you");



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