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

I am adding the random part to the weapons[currentWeapon].power value, but it not working…

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 
  weapons[currentWeapon].power += (Math.floor(Math.random() * xp) + 1)
}

Your browser information:

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

Challenge Information:

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

The final power value is the one that is used to subtract from monsterHealth so it must be calculated before the subtraction.

health -= fixedValue + randomValue

Also, it looks like there might be a regex that won’t allow for the extra parentheses you have around your Math functions.

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

This is the new code, following your suggestion, still won’t work

do not change the power of the weapon, you need to substract two things from monsterHealth

1 Like

It worked!, Thanks a million…

I have the same problem but I can’t find the solution. I need more help please.

welcome @joelri1981 ! please create your own topic to ask for help

If you have a question about a specific challenge as it relates to your written code for that challenge and need some help, click the Ask for Help button located on the challenge (it looks like a question mark). This button only appears if you have tried to submit an answer at least three times.

The Ask for Help button will create a new topic with all code you have written and include a link to the challenge also. You will still be able to ask any questions in the post before submitting it to the forum.

Thank you.

Continue with Previous line:
monsterHealth -= weapons[currentWeapon].power + Math.floor(Math.random() * xp) + 1;

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