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

Tell us what’s happening:

it is asking me to call the lose function, please help on how i do that.

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.

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){
    
  }
}

Your browser information:

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

Challenge Information:

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

1 Like

It is a bit odd that you are asked to call a function that isn’t defined, but that is the case here.

The identifier is lose and you call a function using () so that is all you have to do, call lose inside the if statement (even if that function does not yet exist).

1 Like

To call the lose function when the player’s health (health) drops to zero or below, you can simply add a condition after updating the player’s health inside your attack function. Here’s how you can modify your attack function to call lose when the player’s health reaches zero:

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(); // Call the lose function when the player’s health reaches zero or below
}
}
In the if (health <= 0) block, lose() is called when the player’s health is less than or equal to zero. You can define the lose function elsewhere in your code to handle what happens when the player loses the game. i hope it will work for you

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