Please I need help. This code keeps giving multiple executions of buyHealth, buy weapon and fightDragon functions upon just a click. I don’t where the problem Is coming from. Thanks in anticipation
gold = 50; // Initial amount of gold
let health = 100; // Initial health
let currentLocation = 0; // Index of the current location
let buttonFunctions = ; // Array to store references to button functions
const locations = [
{
name: “Town Square”,
buttonTexts: [“Go to Store”, “Go to Cave”, “Fight Dragon”],
buttonFunctions: [goStore, goCave, fightDragon],
text: “You are in the town square. You see a sign that says ‘Store.’”,
},
{
name: “Store”,
buttonTexts: [“Buy Health”, “Buy Weapon”, “Go to Town Square”],
buttonFunctions: [buyHealth, buyWeapon, goTown],
text: “You enter the store.”,
},
{
name: “Cave”,
buttonTexts: [“Fight Slime”, “Fight Beast”, “Go to Town Square”],
buttonFunctions: [fightSlime, fightBeast, goTown],
text: “You enter the cave. You see some monsters.”,
},
];
// Initialize buttons
const button1 = document.querySelector(“#button1”);
const button2 = document.querySelector(“#button2”);
const button3 = document.querySelector(“#button3”);
// Add event listeners to buttons
button1.addEventListener(“click”, function () {
buttonFunctions0;
});
button2.addEventListener(“click”, function () {
buttonFunctions1;
});
button3.addEventListener(“click”, function () {
buttonFunctions2;
});
// Update function to change the location
function updateLocation(locationIndex) {
const location = locations[locationIndex];
// Remove previous event listeners
button1.removeEventListener(“click”, buttonFunctions[0]);
button2.removeEventListener(“click”, buttonFunctions[1]);
button3.removeEventListener(“click”, buttonFunctions[2]);
// Store references to new button functions
buttonFunctions = [
function () {
location.buttonFunctions0;
},
function () {
location.buttonFunctions1;
},
function () {
location.buttonFunctions2;
},
];
// Add new event listeners
button1.addEventListener(“click”, buttonFunctions[0]);
button2.addEventListener(“click”, buttonFunctions[1]);
button3.addEventListener(“click”, buttonFunctions[2]);
// Update button texts and display location text
button1.textContent = location.buttonTexts[0];
button2.textContent = location.buttonTexts[1];
button3.textContent = location.buttonTexts[2];
document.getElementById(“text”).textContent = location.text;
}
// Example functions for different actions
function goStore() {
currentLocation = 1;
updateLocation(currentLocation);
}
function goCave() {
currentLocation = 2;
updateLocation(currentLocation);
}
function fightDragon() {
console.log(“Fighting dragon”);
}
function buyHealth() {
// Check if there is enough gold to buy health
if (gold >= 10) {
// Subtract 10 from gold and add 10 to health
gold -= 10;
health += 10;
// Update UI to reflect changes
document.getElementById("goldText").textContent = gold;
document.getElementById("healthText").textContent = health;
} else {
// Display a message if there’s not enough gold
document.getElementById(“text”).textContent = “You do not have enough gold to buy health”;
}
}
function buyWeapon() {
console.log(“Buying weapon”);
}
function goTown() {
currentLocation = 0;
updateLocation(currentLocation);
}
function fightSlime() {
console.log(“Fighting slime”);
}
function fightBeast() {
console.log(“Fighting beast”);
}
// Initialize the game at the starting location
updateLocation(currentLocation);