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

Tell us what’s happening:

I am stuck on step 150 about adding an if function. I submitted this code but it just tells me “you should add an if statement”. I don’t know what is wrong with the if statement i added. I have tried different variations of adding the if statement and I am stuck, pls help!

Your code so far

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

/* 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 + ".";
 if isMonsterHit() {
  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();
    }
  }
}

// User Editable Region
/* file: styles.css */

Your browser information:

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

Challenge Information:

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

please click the reset button and try again.
You were supposed to find the line of code that updates the monsterHealth and not the line of code that updates the health.
Also look at the other if statements in your code. They all have parenthesis after the if right?

Thank you. I reset and moved the if statement to the monsterHealth. I tried this but now it is saying that the if statement needs to call isMonsterHit, but I thought this would call isMonsterHit since it is in the parenthesis?

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();
}
}
}

to call a function you have to add parenthesis after the name of the function like attack()

I did it like this and it still doesn’t work…

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();
    }
  }
}

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

oh okay thank you for editing it

do you remember how I said that if statements need parenthesis before?
When you look at the code in your editor, do you see a red line below a specific if statement? It needs to be corrected.

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