Learn Basic JavaScript by Building a Role Playing Game Question Step 63 - location vs locations

Hi All,

I have reviewed the forum but still do not get it how and why function update (location) is used to get const locations = object as function update(locations), just swaps the specific lines when locations called?

Could you please describe me as if was a 5 years old child? :slight_smile:

Thank you in advance for your help.

I am here at the moment:

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

function update(location) {
    button1.innerText = location["button text"[0]];
    button2.innerText = "Go to cave";
    button3.innerText = "Fight dragon";
    button1.onclick = goStore;
    button2.onclick = goCave;
    button3.onclick = fightDragon;
    text.innerText = "You are in the town square. You see a sign that says \"Store\".";
};

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

Welcome to the forum @YoungZeeZee

Firstly, five year olds learn faster than adults. They average about ten new words each day.

Most adults learn only one new word each day, until middle age.

A function is a way of writing repeatable code.

For example consider the following function:

  function helloName(name) {
    console.log("Hello " + name + ", welcome to the forum.");
}

This code can be used to output a message to the console, with a different name each time the program is run.

Calling the function, …

  helloName("YoungZeeZee")

will output the string.

“Hello YoungZeeZee, welcome to the forum.”

name is a parameter for the function.
Think of it as placeholder code.

"YoungZeeZee" is known as the argument, the code that is actually passed to the function.

If nothing is passed to the function call, then name is undefined

So, the update function is running code, depending on the argument.

This will update the button text and the click properties of the buttons, in later steps. For now, you are just updating the first line of the function.

Here is the complicate bit.

The goTown function, when called by pressing the appropriate button at the appropriate time, passes the locations array at index 0 to the update function.

Each time location is mentioned in the update function, it is referencing …

from the goTown function.

So, in your code, you don’t need [0]

as the function replaces location with the argument from the goTown function.

Happy coding

2 Likes

It was super easy to understand.
Thank you for your kind help!

after several steps location looks like this

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
};

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