Why in step 45 in Role Playing Game it is asked to update the functions?

I solved this step but I don’t understand why in step 45 it is asked to update the functions?

"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."

Before, the buttons when clicked were represented by the variables goStore, goCave and fightDragon

function goStore(){
button1.onclick = goStore;
button2.onclick = goCave;
button3.onclick = fightDragon;
....................................
....................................
}

In Step 45:

the buttons when clicked are represented by the functions buyHealth , buyWeapon , and goTown and this code is still written within the function goStore()

why?

Can you please provide a link to the exercise?

@ytrkptl sure, here is the link to step 45 https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures-v8/learn-basic-javascript-by-building-a-role-playing-game/step-45

Ok, so in that exercise itself, you actually don’t even have to define these functions. The tests that run do not check for this. You mentioned:

In Step 45:

the buttons when clicked are represented by the functions buyHealth , buyWeapon , and goTown and this code is still written within the function goStore()

why?

You are simply assigning the buttons these functions that you will actually implement as you move to Step 47. You are assigning a different behavior to these buttons when the goStore button is clicked. That way you can make sure that buyHealth, buyWeapon, and goTown functions only run when the goStore button is already clicked once and the buttons display “Buy 10 health (10 gold)”, “Buy weapon (30 gold)”, and “Go to town square”. I hope this answers your question. If not, please let me know and I will try my best to clarify further.

//See my solution below

function goStore() {
button1.innerText = “Buy 10 health (10 gold)”;
button2.innerText = “Buy weapon (30 gold)”;
button3.innerText = “Go to town square”;
button1.onclick = buyHealth;
button2.onclick = buyWeapon;
button3.onclick = goTown;
}

/* The problem with the instruction they do not tell whether buyHealth, buyWeapon and goTown are strings or functions but indeed they’re functions*/

1 Like

well they wort run so you gotta understand it’s a function

why this answer isn’t accepted
function goStore() {
button1.innerText = “Buy 10 health (10 gold)”;
button2.innerText = “Buy weapon (30 gold)”;
button3.innerText = “Go to town square”;
button1.onclik = buyHealth;
button2.onclik = buyWeapon;
button3.onclik = goTown;
};
it keeps saying you have to update button1 to buyHealth

you didn’t wrote onclick correctly

function goStore(){
button1.onclick = buyHealth();
button2.onclick = buyWeapon();
button3.onclick = goTown();
}

This is the solution. Thank You!

1 Like

We do not allow people to post solution code but in this case, as it isn’t correct I will leave it.

The test needs to be updated so your code will correctly fail the test (PR is in progress).


You should not call () the handler functions. Doing so will break the handler.

It should be

someBtn.onclick = someFn;

and not

someBtn.onclick = someFn();

The first assigns the function so it can be called by the onclick event, the second assigns the return value of calling the function, which is not the function, so the onclick event will not have a function to call.

As I’m going through this exercise I confirm the directions are confusing since it is asked to update the function()