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

Tell us what’s happening: The prompt is a little unclear and the error message isn’t helping in terms of what I’m doing wrong. Need to decrement currentWeapon.

Your code so far

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;
    text.innerText += " It's a hit!";
    weapons[currentWeapon].power--;    
  } else {
    text.innerText += " You miss.";
  }
  healthText.innerText = health;
  monsterHealthText.innerText = monsterHealth;
  if (health <= 0) {
    lose();
  } else if (monsterHealth <= 0) {
    if (fighting === 2) {
      winGame();
    } else {
      defeatMonster();
    }
  }
  if (Math.random() <= .1) {
    text.innerText += " Your " + inventory.pop() + " breaks.";

  }
}

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 154

Here’s a hint:

You just need to apply the same logic

Apply the same logic to a different value? I am assuming the power of the weapon needs to decrease as it is used to fight the monster; I don’t think there is another value in currentWeapon that could be decremented.

What syntax did you use to reduce the power of weapons[currentWeapon]?
Apply that syntax to reducing currentWeapon

Currently using

weapons[currentWeapon--];

It doesn’t seem to matter which if block I put it in, the code still does not pass.

Do the instructions mention weapons or just currentWeapon?

1 Like

I believe you must decrement current weapon not power. After our weapon breaks you must decrement to previous weapon

Actually it should be
answer redacted
This works …after the updated text in your last if statement

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.

Understood. Thank you.

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