I customized the dragon slayer game, but I want to optimize my code

game

#1

After the dragon slayer game was completed, I decided to tweak but I just repeated a lot of my code so I'm sure this can be optimized somehow. Any help and pointers would be appreciated.

var slaying = true;
var youHit = Math.floor(Math.random() * 2);
var totalDamage = 0;
var playerHp = 100;
var dragonHp = 100;

function dragonSlayer() {
    while(slaying) {
        var damageThisRound = Math.floor(Math.random() * 50 + 1);
        console.log("Player HP: " + playerHp);
        console.log("Dragon HP: " + dragonHp);
        if(youHit) {
            totalDamage += damageThisRound;
            console.log("You did " + totalDamage + " damage!");
            dragonHp -= totalDamage;
            if(dragonHp <= 0) {
                console.log("Player HP: " + playerHp);
                console.log("Dragon HP: 0");
                console.log("YOU WIN!");
                slaying = false;
            } else {
                totalDamage = 0;
                youHit = Math.floor(Math.random() * 2);
            }
        } else {
            totalDamage += damageThisRound;
            console.log("The dragon did " + totalDamage + " damage!");
            playerHp -= totalDamage;
            if(playerHp <= 0) {
                console.log("Player HP: 0");
                console.log("Dragon HP: " + dragonHp);
                console.log("YOU ARE DEAD!");
                slaying = false;
            } else {
                totalDamage = 0;
                youHit = Math.floor(Math.random() * 2);
            }
        }
    }
};

dragonSlayer();

#2

I got some help at stackexchange and thought I'd share the optimized code:

function slayDragon(playerHp, dragonHp) {
  var playerTurn;
  var damageThisTurn;
  
  while(true) {
    console.log("Player HP: " + Math.max(playerHp, 0));
    console.log("Dragon HP: " + Math.max(dragonHp, 0));
    
    //End conditions
    if(playerHp <= 0) {
      console.log("YOU ARE DEAD!");
      break;
    } else if(dragonHp <= 0) {
      console.log("YOU WIN!");
      break;
    }
    
    //Who gets to swing?
    playerTurn = Math.floor(Math.random() * 2);
    damageThisTurn = Math.floor(Math.random() * 50 + 1);
    
    if( playerTurn ) {
      console.log("You do " + damageThisTurn + " damage.");
      dragonHp -= damageThisTurn;
    } else {
      console.log("Dragon does " + damageThisTurn + " damage.");
      playerHp -= damageThisTurn;
    }
    
    //Spacing
    console.log("");
  }
}

slayDragon(100, 100);

#3

var slaying = true;
var youHit = Math.floor(Math.random() * 2);
var damageThisRound = 3;
var totalDamage = 0;
var playerDamage = 0;
var dragonHit = Math.floor(Math.random() * 2);

while(slaying) {

dragonHit = Math.floor(Math.random() * 2);
youHit = Math.floor(Math.random() * 2);

if(youHit === 1) {
var damage = Math.round(Math.random() * 3 );

if(damage === 0) {
    console.log("you hit the dragon but didn't do any damage");
}
else {
    console.log("You dealt " + damage + " to the dragon.");
    totalDamage += damage;
    if(totalDamage > 6) {
        totalDamage = 6;
    }
    console.log("Dragon damage: " + totalDamage);

    if(totalDamage >= 6) {
        console.log("You have defeated the dragon");
        slaying = false;
        dragonHit = 0;
    }
}

}
else if (playerDamage < 6){
console.log("You missed the dragon!");
}

if(dragonHit === 1) {
var damage = Math.round(Math.random() * 3 );

if(damage === 0) {
    console.log("the dragon hit the player but didn't do any damage");
}
else {
    playerDamage += damage;
    if(totalDamage > 6) {
        totDamage = 6;
    }
    console.log("The dragon dealt " + damage + " to the player.");
    console.log("Player damage: " + playerDamage);

    if(playerDamage >= 6) {
        console.log("You have been defeated");
        slaying = false;
        dragonHit = 0;
    }
}

}
else if (totalDamage < 6){
console.log("The dragon missed you!");
}
}

Try this code. I made it and it works really well. BTW: leave some advice if u want.