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

Tell us what’s happening:

i have to decrease the value of currentWeaponIndex after i update the text. so i did update the text with: text.innerText += " You hit."; en vervolgens de currentWeaponIndex --; what i do wrong?

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()) {
    monsterHealth -= weapons[currentWeaponIndex].power + Math.floor(Math.random() * xp) + 1;    
    text.innerText += " You hit it.";
    currentWeaponIndex--;     
  } 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.";

  }
}

// User Editable Region

Your browser information:

User Agent is: Mozilla/5.0 (X11; CrOS x86_64 14541.0.0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/126.0.0.0 Safari/537.36

Challenge Information:

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

Look closely at the example for how to decrease the value of a variable by 1. It tells you the variable you need to decrease inside your if statement.

let num = 10;
num--;

I am not able to see what you have written as you have not posted any code. Please post your code updated with what you have tried.

I see now that you have updated the wrong if statement. Try re-setting and only update the last if statement.

No, @Def1986 did the correct syntax for decreasing currentWeaponIndex. The problem is that he put it at the end of the first if statement, not the last.

Welcome to the freeCodeCamp Forum, @Def1986!

The context for this step, is in the previous step. In the previous step you updated the text for the weapon breaking, so now after that text update add the decrement to currentWeaponIndex. Do not change anything else.

Also, there is an empty line in the starting code for this step, that is where the new code should go.