Learn Basic JavaScript by Building a Role Playing Game - Step 150

Tell us what’s happening:

The monsterHealth update has been placed as requested in an if block that has the condition isMonsterHit as requested by the tutor prompt. This if block has been added after a previous if statement. Does the question expect the monsterupdate to be placed within the previous if statement or?

Your code so far

<!-- file: index.html -->

/* file: styles.css */

/* file: script.js */
// User Editable Region

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

// User Editable Region

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/131.0.0.0 Safari/537.36

Challenge Information:

Learn Basic JavaScript by Building a Role Playing Game - Step 150

Hi there!

Reset the challenge step. And update your existing line number 185 that has monsterHealth variable assignment. By adding an if statement around it with the condition isMonsterHit

Like this?:

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

isMonsterHit is a function, you need to call it.

So what would be the if condition?

Here is whats done so far:

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

Don’t call the function within the if {} block. Call it within the if condition () by adding () to it.

Like this?:

line 185

  if(isMonsterHit) {monsterHealth -= weapons[currentWeaponIndex].power + Math.floor(Math.random() * xp) + 1;
  }

No, you are not calling the function isMonsterHit within the if condition. You only have reference to isMonsterHit. Make that function reference to a call to the function by adding () Infront of it.
Remember the if () braces should be still there along the function call.

did you manage to solve this step?

Oh yes I did. Thank you all