Step 126 Build a Role Playing Game

Step 126

You can make an else statement conditional by using else if. Here’s an example:

if (num > 10) {

} else if (num < 5) {

}

At the end of your if statement, add an else if statement to check if monsterHealth is less than or equal to 0. In your else if, call the defeatMonster function.

My code so far:

function attack() {
  text.innerText = "The " + monsters[fighting].name + " attacks.";
  text.innerText += " You attack it with your " + weapons[currentWeapon].name + ".";
  health -= monsters[fighting].level;
  monsterHealth -= weapons[currentWeapon].power + Math.floor(Math.random() * xp) + 1;
  healthText.innerText = health;
  monsterHealthText.innerText = monsterHealth;
  if (health <= 0) {
    lose();
  } else if {
    monsterHealth <= 0;
    defeatMonster();
  }
}

The button is not working, I’ve reloaded and reset the lesson several times. Can someone check to see if I at least have the code correct.

Hey @veritegreeneyes2000,

When you create an “else if” statement, it has to have a condition, the same as an if statement. The only time this isn’t the case is when you write just an “else” statement which is meant to account for anything that wasn’t checked before.

So, the condition for the “else if” should check the monsterHealth variable to see if it is less than or equal to 0. Be sure to use the same syntax that they mention in the challenge instructions.

Thank you! I’m putting the statement in the wrong place… again. Of course, now it works, including the button!