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

Tell us what’s happening:

Hi, I can’t figure out how to fix this, please help

Your code so far

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

/* file: styles.css */

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

function buyWeapon() {
if (currentWeaponIndex < 3)
{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 (Macintosh; Intel Mac OS X 10_15_7) 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

Your code is not well-formatted, so it’s difficult to see how the curly brackets pair up. Your issue is that your if statements are not correctly nested and you’re missing a closing curly bracket too.

Here’s how your code looks if I format it more clearly:

function buyWeapon() {
  if (currentWeaponIndex < 3) {
    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.";
  }

Your else statement should belong to the if (gold >= 30) condition but it’s factored to belong to the first condition instead. Also, if you look at the pairing of the brackets, there’s no closing bracket for the function as a whole.

2 Likes

That’s 100% different than step 95 which I’m asking for help!
Anyway good luck!

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