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

Hi everyone,
I’m Tamara from Argentina

I’ve had some trouble throughout the whole challenge. I just keep getting nothing else than the Check your code button, no comments or hints at all. I tried to open and close the console and after I do sometimes I get some weird error messages in the hint section of the page such as:

"Sorry, your code does not pass. Keep trying.

SyntaxError: unknown: Unexpected token (100:6) 98 | } else { 99 | text.innerText = “You do not have enough gold to buy a weapon.”; > 100 | } else { | ^ 101 | text.innerText = “You already have the most powe rful weapon!”; 102 | } 103 | }"

In this case I purposely wrote “powe rful” incorrectly to see if there were any changes. Once I corrected it, this message popped.

Step 99

Add an else statement for your outer if statement. Inside this new else statement, set text.innerText to You already have the most powerful weapon!.

I think my code looks correct since it’s quite similar to the previous step, I literally have an example two lines on top.

I’ll try tro paste my code properly:

let xp = 0;
let health = 100;
let gold = 50;
let currentWeapon = 0;
let fighting;
let monsterHealth;
let inventory = ["stick"];

const button1 = document.querySelector('#button1');
const button2 = document.querySelector("#button2");
const button3 = document.querySelector("#button3");
const text = document.querySelector("#text");
const xpText = document.querySelector("#xpText");
const healthText = document.querySelector("#healthText");
const goldText = document.querySelector("#goldText");
const monsterStats = document.querySelector("#monsterStats");
const monsterName = document.querySelector("#monsterName");
const monsterHealthText = document.querySelector("#monsterHealth");
const weapons = [
  { name: 'stick', power: 5 },
  { name: 'dagger', power: 30 },
  { name: 'claw hammer', power: 50 },
  { name: 'sword', power: 100 }
];
const locations = [
  {
    name: "town square",
    "button text": ["Go to store", "Go to cave", "Fight dragon"],
    "button functions": [goStore, goCave, fightDragon],
    text: "You are in the town square. You see a sign that says \"Store\"."
  },
  {
    name: "store",
    "button text": ["Buy 10 health (10 gold)", "Buy weapon (30 gold)", "Go to town square"],
    "button functions": [buyHealth, buyWeapon, goTown],
    text: "You enter the store."
  },
  {
    name: "cave",
    "button text": ["Fight slime", "Fight fanged beast", "Go to town square"],
    "button functions": [fightSlime, fightBeast, goTown],
    text: "You enter the cave. You see some monsters."
  }
];

// initialize buttons
button1.onclick = goStore;
button2.onclick = goCave;
button3.onclick = fightDragon;

function update(location) {
  button1.innerText = location["button text"][0];
  button2.innerText = location["button text"][1];
  button3.innerText = location["button text"][2];
  button1.onclick = location["button functions"][0];
  button2.onclick = location["button functions"][1];
  button3.onclick = location["button functions"][2];
  text.innerText = location.text;
}

function goTown() {
  update(locations[0]);
}

function goStore() {
  update(locations[1]);
}

function goCave() {
  update(locations[2]);
}

function fightDragon() {
  console.log("Fighting dragon.");
}

function buyHealth() {
  if (gold >= 10) {
    gold -= 10;
    health += 10;
    goldText.innerText = gold;
    healthText.innerText = health;
  } else {
    text.innerText = "You do not have enough gold to buy health.";
  }
}

function buyWeapon() {
  if (currentWeapon < weapons.length - 1) {
    if (gold >= 30) {
      gold -= 30;
      currentWeapon++;
      goldText.innerText = gold;
      let newWeapon = weapons[currentWeapon].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.";
    } else {
      text.innerText = "You already have the most powerful weapon!";
    }
  }
}

Does anyone know how to fix this?

Thank you so much in advance!

Hello, copy and paste the url to the exercise so it can be check inside the editor.

Hi! Here it is:

https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures-v8/learn-basic-javascript-by-building-a-role-playing-game/step-99

Hey, you just need to move down the else statement so it has an empty space. so two curly braces then the else statement after that two closing braces.

      text.innerText = "You do not have enough gold to buy a weapon.";
    
    } 
    
  }else{
    text.innerText = "You already have the most powerful weapon!";
  }
}
2 Likes

I’m sorry, I’ve just tried adding spaces and braces everywhere but I can’t get it right, still no hints or any sign of life so I don’t even know if the page is working at all

Here is what this should look like.

1 Like
function buyWeapon() {
  if (currentWeapon < weapons.length - 1) {
    if (gold >= 30) {
      gold -= 30;
      currentWeapon++;
      goldText.innerText = gold;
      let newWeapon = weapons[currentWeapon].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.";
    }
  } // <-- this is end of the outer if, that is where you add the else
}
1 Like

Hello I have set my 1st project named Gacha Lemon. Can you please give me some suggestions on how to set and improve JavaScript?

1 Like

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