JavaScript role playing game

Please in the JavaScript role playing game project (step 81). I tried running the code in my browser (chrome) having saved in VS code but the buyHealth function gets executed multiple times after the first click. Please what could be wrong. Thanks in anticipation

et 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 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.”
}
];

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() {

}

function fightSlime() {

}

function fightBeast() {

}

Hi @Embraz

You have not declared the weapons variable.

Also, the quotes for Store (shown at the end of the code block) need to be escaped.

Noticed funky quotes in the code you posted.

Okay I’d update that but what about the multiple executions of the buyHealth function I keep getting. I even had to copy code from the exercise ran it on my browser and it didn’t work. Could it be a challenge from my browser?

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