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

Tell us what’s happening:

I can not pass this test. It asks me to set the currentWeaponIndex less than 3
and this my statement if(currentWeaponIndex < 3)

Your code so far

<!-- file: index.html -->

/* file: styles.css */

/* file: script.js */
// User Editable Region

if(currentWeaponIndex < 3){
      function buyWeapon() {
        if (gold >= 30) {
        gold -= 30;
        currentWeaponIndex++;
        goldText.innerText = gold;
        let newWeapon = weapons[currentWeaponIndex].name;
        text.innerText = "You now have a " + newWeapon + ".";
        inventory.push(newWeapon);
        text.innerText += " In your inventory you have: " + inventory;
      } else {
        text.innerText = "You do not have enough gold to buy a weapon.";
      }
    }
}



// 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

Challenge Information:

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

It is asking you to wrap the code inside the function in the if statement, not the function itself.

Wrap all of the code in your buyWeapon function inside another if statement.

yeah, that’s right, thank you.
I should read carefully.

1 Like

Just as an aside. You will pretty much never surround function definitions with conditional code, it doesn’t really serve much purpose. I suppose it might be used as some sort of odd optimization to avoid function definitions that aren’t needed. But I don’t think I have ever seen that.

If it was a function returning a function, then you might see the return function definition inside a condition.

Anyway, happy coding.

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