JavaScript building a role playing game 64

Why does the variable “locations” get changed when passing “location” into the update function? or is this just a textual error?

location is a parameter which represents an object from the locations array.

You can see for yourself when you add this console statement and call the function here

function update(location) {
  console.log(location)
  button1.innerText = "Go to store";
  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\".";
}
update(locations[0])

locations is an array of objects

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

location is a parameter for the update function.
a parameter acts as a placeholder for the real value for when a function is called.
location represents and object in the locations array.

when you call the update function like here

update(locations[0])

that is where you pass in an argument which is going to be the real value used in that update function.
in this case, locations[0] is the first object from the locations array.

hope that helps

1 Like

Thank you. I guess I am confused as to why the letter “s” gets dropped when adding location as the parameter to the update function. Sorry for my confusion.

so the parameter name technically could have been anything.

this technically works and has the same functionality

function update(someParamName) {
  console.log(someParamName)
  button1.innerText = "Go to store";
  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\".";
}

so location was just the name that the team went with.
but technically, you could name it anything and still get the same desired functionality.
when naming things, you want to name something that makes sense for the intended purpose of that function or variable.

that is why location is used for this parameter name.

hope that is clearer

Oh, ok. Thank you. I think I’m getting it. :grinning:

1 Like