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

Tell us what’s happening:

what could be wrong with my code here?
I am stuck here

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);
  if (isMonsterHit) {
    function 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();
    }
  }
}

// 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/126.0.0.0 Safari/537.36 Edg/126.0.0.0

Challenge Information:

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

1 Like

Hi @eduguvasu

The two lines indicated in the above code block are not needed, as the function is already defined.

Also, the condition needs to call the function.

Happy coding

Hi there,

This is what the instruction asked:

find the line of code that updates the monsterHealth variable and place it within an if block with a condition that calls the isMonsterHit function.

There are 2 things wrong with your code:

First, you put the line of code that updates the monsterHealth variable inside the definition of the isMonsterHit function.

The line:

function isMonsterHit() {
};

is used to define function.

The instruction didn’t ask to define isMonsterHit function.
It asked to call isMonsterHit function as the condition statement of the if.


Secondly, the condition of your if statement:

if (isMonsterHit) {
}

isMonsterHit is just the function name, it isn’t a function call.

Let me elaborate with an example:

This is a function definition :

function sayHi() {
    return "Hello";
}

This is a function call:

sayHi()

This is just the function name:

sayHi

The function name sayHi will return nothing.
But the function call sayHi() will return a string: "Hello".

2 Likes

i have rewrote my code to this but still not working,
if (isMonsterHit) {
isMonsterHit()
monsterHealth -= weapons[currentWeaponIndex].power + Math.floor(Math.random() * xp) + 1

};

Hi @eduguvasu

The indicated line is the correct way to call a function. Replace the if condition with that code.

Happy coding

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

I tried this still not working.

Hi @eduguvasu ,

You should delete LINE 1

Put () after isMonsterHit on LINE 2 to make it a function call.

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

Still i do not pass. Not working

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

While this one tells me to add an if statement. Where do i add the if statement?

I figured it out thank you.

1 Like

Hi, the answer is: :grinning:

MOD Solution removed

Hi @yamejia088

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

Hi,@eduguvasu .

code removed by moderator

hi @ysilkov

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.