JavaScript dragon slayer game (Codecademy) I'm currently doing the Javascript course at Codecademy, and decided to customize their dragon slayer game. I would appreciate any help/pointers that can help me optimize the code. There's a lot of repetition that I'm sure can be done in a smarter way.

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!");
            console.log(" ");
            dragonHp -= totalDamage;
            if(dragonHp &lt;= 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!");
            console.log(" ");
            playerHp -= totalDamage;
            if(playerHp &lt;= 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();
Added benefit of function

The function does not seem to have any added benefit right now. For example, running the function twice will not print two "fights". This is mainly because you use global variables. The function is useful if this was a class, where you can set and reset the health of entities. In this case I would just pass them as parameters.

Performance

Performance is about the speed at which the code executes. I do not see flaws which should hamper execution speed too much.

DRY

Your code can be optimized by noticing that the if and the else-statement are nearly identical. You can optimize it by extracting that code and making that it's own function.

Now identify that the log-lines with hp for each entity are duplicated 3 times, twice to tell if we won or not. We can do better. Let's just do it once, and break out of the loop with break when we detect a final conclusion.

The function now does not much useful. We can just put those 2 lines in the first function itself.

For more information, see Wikipedia

Weird damage calculation

totalDamage += damageThisRound;
console.log("You did " + totalDamage + " damage!");
console.log(" ");
dragonHp -= totalDamage;


What is this supposed to do? You reset totalDamage every time, except in a winning/losing condition in which case this code is never executed again.

Variable hoisting

Javascript has a feature called variable hoisting. All variables defined in a context are moved to the top of that context before any other code is executed in that context. I recommend to move all declarations to the top of the context to better match what the code does.

Improvement



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 &lt;= 0) {
      console.log("YOU ARE DEAD!");
      break;
    } else if(dragonHp &lt;= 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);