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

Hello! Good day! I am asking for clarification regarding this step. Not in the code I wrote but in how JavaScript works in general. I do not know if it’s going to be explained at a later step but I am having a hard time wrapping my head around the concept posed with the instructions which reads as follows:

Step 83

The value of the currentWeapon variable corresponds to an index in the weapons array. The player starts with a "stick", since currentWeapon starts at 0 and weapons[0] is the "stick" weapon.

My question is why? How or why does the currentWeapon variable correspond to an index in the weapons array? Why does xp (which is also valued at 0) not point to the weapons array? I can’t seem to find a line in the code that would make the currentWeapon point to weapons array. Please help. Thank you!

Hello and good question but its the value of the currentWeapon = value there referencing. I hope this answers or sheds some light on your inqury.

Welcome to the forum @AspirantCoder

The value of the currentWeapon variable corresponds to an index in the weapons array. The player starts with a "stick" , since currentWeapon starts at 0 and weapons[0] is the "stick" weapon.

The value of currentWeapon is the index used to access the weapons array.

For example if currentWeapon is 0, then the value of weapons[currentWeapon] is the first object in the array.

Happy coding

Thank you for your reply.

Yes, I think I understand what you mean but if the value of the currentWeapon is the index, how come the value of xp is not when it’s the same value? Also, let’s say for example I wrote another array like this under the weapons array like so:

const weapons = [
  { name: 'stick', power: 5 },
  { name: 'dagger', power: 30 },
  { name: 'claw hammer', power: 50 },
  { name: 'sword', power: 100 }
];

const armaments = [
  { name: 'bazooka', power: 1000 },
  { name: 'trident', power: 412 },
  { name: 'heartstopper', power: 1400},
];

Without changing anything on the code as written from the tutorial, how would the currentWeapon know to refer to the weapons array instead of the armaments array? Sorry, I don’t know if I’m making any sense.

In the buyWeapons function, your code is incrementing the value of currentWeapon.

currentWeapon does not refer to the weapons array. It is used as a counter. The more weapons you buy, the greater the value of currentWeapon becomes. Later on, you write code for when your weapon can also break, which decrements the counter.

The arrays have different names, so armaments is not accessed at any time.

The xp variable refers to experience points. They start at zero. You gain these by vanquishing monsters.

This is a project based learning course, so a lot of things won’t make sense until you finish the code.

Happy coding

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