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

Tell us what’s happening:

Hello everyone, kindly walk me through with this. The instruction is below;
" In your attack function, below the health variable, create an if statement. Set the condition to call the isMonsterHit function"

My code is below;

function attack() {
  text.innerText = "The " + monsters[fighting].name + " attacks.";
  text.innerText += " You attack it with your " + weapons[currentWeapon].name + ".";
  health -= getMonsterAttackValue(monsters[fighting].level);
if (isMonsterHit) ();
  monsterHealth -= weapons[currentWeapon].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();
    }
  }
}

Your code so far

WARNING

The challenge seed code and/or your solution exceeded the maximum length we can port over from the challenge.

You will need to take an additional step here so the code you wrote presents in an easy to read format.

Please copy/paste all the editor code showing in the challenge from where you just linked.

Replace these two sentences with your copied code.
Please leave the ``` line above and the ``` line below,
because they allow your code to properly format in the post.

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.2.1 Safari/605.1.15

Challenge Information:

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

Just as the instructions suggest, create an if statement, and call the said function as the if statement condition. Your if statement syntax is incorrect. Here is the correct syntax

if (condition) { 
code
}

Also, check how you called the function, that is not the right way.

Thanks for your patience; what’s the if statement condition? my code is below;

function attack() {
  text.innerText = "The " + monsters[fighting].name + " attacks.";
  text.innerText += " You attack it with your " + weapons[currentWeapon].name + ".";
  health -= getMonsterAttackValue(monsters[fighting].level);
  if () {
  isMonsterHit();
}
  monsterHealth -= weapons[currentWeapon].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();
    }
  }
}

Hi @Answer_Alyosha

Set the condition to call the isMonsterHit function.

The body of the if statement should be empty.

Happy coding

Hello appreciate your reply. Is there something I’m missing?

  text.innerText += " You attack it with your " + weapons[currentWeapon].name + ".";
  health -= getMonsterAttackValue(monsters[fighting].level);
  if {
  isMonsterHit();
}

The function call goes in the condition of the if statement.
The body of the statement should be empty.

@stephenmutheu gave the basic syntax.

Where is the condition in this if statement?

That’s the thing Jeremy. I don’t understand what the condition is.

Hi @Answer_Alyosha

Set the condition to call the isMonsterHit function

I came up with this; what are my missing?

 text.innerText += " You attack it with your " + weapons[currentWeapon].name + ".";
  health -= getMonsterAttackValue(monsters[fighting].level);
  if (condition) {
  isMonsterHit();
}
  1. This needs to be replaced with the function call.
  1. The body of the if statement needs to be empty.

Thanks for your patient; how about this?


  if isMonsterHit() {
  
}

Why did you delete the ()s around where the condition goes?

Replace the conditions (conditions) with isMonsterHit . And leave { } brackets empty.

Thanks for your assistance Hasan; I tried it but I seemingly I ain’t doing something right.

;
  if (isMonsterHit) {
    
  };

You maybe deleted some Syntex of your code.

And around your if statement that semicolons is extra. Reset your challenge and try again.
@Answer_Alyosha

Thanks for your patience. I’ve reset my challenge and tried again but same error.

  health -= getMonsterAttackValue(monsters[fighting].level);
if (isMonsterHit) {
  
}
  monsterHealth -= weapons[currentWeapon].power + Math.floor(Math.random() * xp) + 1;
  healthText.innerText = health;

You need to place your new if statement in above line I mentioned. You added it in wrong place.
@Answer_Alyosha

I tried it and got the same result;

 if (health <= 0) {
    lose();
  } else if (monsterHealth <= 0) {
    if (fighting === 2) {
      winGame();
    } else {
      defeatMonster();
    }
  } if (isMonsterHit);
}

This isn’t like the example you were shown. Also, you are not calling the isMonsterHit function.