Learn Basic JavaScript by Building a Role Playing Game - Step 83

Tell us what’s happening:

I am on step 83, and I have realized there is something I can not wrap my head around. How is the script connecting some of these arrays to future functions?

I am sure I am just an idiot lol but I really don’t get it.

How does the code know that the “locations” array is what should be referenced for location[0] and it should pull object 1 of locations array?

Same with “currentWeapon” at the top, how does the “weapons” array later get pulled so that “currentWeapon = 0” ?

Your code so far

<!-- file: index.html -->

/* file: styles.css */

/* file: script.js */
// User Editable Region

function buyWeapon() {
  if (gold >= 30) {
    gold -= 30;
  }
}

// User Editable Region

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36

Challenge Information:

Learn Basic JavaScript by Building a Role Playing Game - Step 83

Firstly, it’s not about being an idiot; it’s more about being curious and inquisitive. I think the lessons are designed for this purpose. You should ask questions and try to understand. There’s nothing wrong with that. Okay, let’s try to understand then:

let currentWeapon = 0;

This line of code assigns a numeric value of 0 to the currentWeapon variable. This line alone doesn’t indicate which weapon is currently selected. However, the context where this variable is used can provide more information about its purpose. The lesson seems to explain that arrays contain elements with index numbers. You can access and use elements in the array using these index numbers. If you are going to store all weapons in an array, then what should be the index number of the first element representing a weapon? Try to write these on console for example.

weapons[currentWeapon]
weapons[currentWeapon].name

When you provide an index ‘number’ to an array, the array will return the corresponding element. This is the logic of JavaScript; an array’s elements can be accessed using index numbers, similar to locations array too.