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

i am having trouble trying to complete this step. it says to update the text of buttons using onclick but when i do it. it does not do anything

my code so far

" 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”);

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

function goStore() {

button2.onclick = “buyWeapon”;
button3.onclick = “goTown”;

}

function goCave() {
console.log(“Going to cave”);
}

function fightDragon() {
// console.log(“Fighting dragon.”);
button3.onclick = “goTown”;
}"
and what the instruction says " You will also need to update the functions that run when the buttons are clicked again.

In your goStore() function, update the onclick property for each button to run buyHealth, buyWeapon, and goTown, respectively. "

Hello:)

Just to make your code more readable:

1 Like

You have to put three backticks ``` before and after the code :slight_smile:

Hello:)
You should keep the already written code as it is. You have to keep the goStore function as it is:

function goStore() {
  button1.innerText = "Buy 10 health (10 gold)";
  button2.innerText = "Buy weapon (30 gold)";
  button3.innerText = "Go to town square";
}

And then you have to add within the function,
as the step says: “update the onclick property for each button to run buyHealth , buyWeapon , and goTown , respectively.”

thank you for replying , so what you mean is that i have to add another line with the onlock property ?

It is easy to update the onclick property for each button, cause you have done onclick already in lines 21, 22, 23.

button1.onclick = goStore;
button2.onclick = goCave;
button3.onclick = fightDragon;

If you click on gostore button, it is going to update all three buttons inner text already. With updating the onclick to : buyHealth , buyWeapon , and goTown , you will add in the future some functions and then sth going to happen:)

You have three buttons, so you have to add three more lines within the function.

Here is the first one:

button1.onclick = buyHealth;
1 Like

thank you so much, i now kmnow what i was doing wrong

thank you so much ^^ :heart:

Your buttons originally has three functions:
goStore, goCave, fightDragon

Whithin this function you update this to:
buyHealth , buyWeapon , and goTown

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