Learn Basic JavaScript by Building a Role Playing Game

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

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

Hi guys, I’m starting my javascript curriculum and I have a question regarding the script i quoted above.

In the update function, the parameter is “location” while in the goTown function, the parameter of the update function is “locations”. I’m confused because aren’t they supposed to be case-sensitive ? Based on my understanding, I would think that it should be “function update(locations)” instead of “function update(location)”

Can someone help me explain why ? Thank you.

Welcome to the forum @andreywjaya

One is a variable, the other is a parameter.

The location parameter is passed into a function, and acts as placeholder code.
The locations variable is referring to the array.

When the goTown function is called, it executes the code in the update function, passing locations[0] as an argument.

Then in the update function the location parameter is replaced with locations[0], thereby updating the button element text and click properties.

Happy coding

Sure thing, it can be confusing. The parameter in the function is referring to a singular location, the location passed in the function call is from an array containing many locations

Hi Teller and Brandon, thank you so much for the reply. I didn’t expect one so soon… Anyways I think I’m starting to get it. So the location parameter and locations variable aren’t really related ? I have a follow up question…

"
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;
"
In the update function, aren’t we supposed to extract the onclicks and innerText from the locations variable ? So for example, why is it location[“button text”][0]
instead of
locations[“button text”][0] ?

Thank you

1 Like

I tested a few things out and can i be corrected if i’m wrong ?

So when i called for the goTown function, the location parameter is replaced with locations[0], which is then transferred to every “location” in the update(location) function ?

1 Like

You are essentially correct in your last comment. To help solidify this:

function printThis(someString) {
  console.log(someString)
}`
const myString = "Hello World"
printThis(myString);

someString is a parameter of the printThis() function, it is assigned the value of the argument you pass to the function when you call it, in this instance someString is given the value of myString just as location is assigned the value of locations[0] when you call update(locations[0])

so the location parameter has the value of locations[0] when update(locations[0]) is called and you can access any data or functions of the locations[0] object through the location parameter in the function definition. That’s why location["button text"][0] can access the value stored at locations[0]["button text"][0] when the function is executed.

Thank you for clarifying! So in your example, console is going to print myString which is “Hello World”? I guess i got thrown off because the project uses location and locations lol. Thanks again!

2 Likes