Learn Basic Javascript by Building a Role Playing Game - Step 118

Hi I think I am starting to get a little lost. I solved the problem again but I am not sure how the currentWeapon section of my code comes into play. In the code above the challenge it does not seem to have a variable that updates and I am just a little confused.

Here is the challenge question

" On a new line, add the string You attack it with your <weapon>. to the text value, replacing <weapon> with the player’s current weapon."

Here is the code I submitted

function attack() {
  text.innerText = "The " + monsters[fighting].name + " attacks.";
  text.innerText += " You attack it with your " + weapons[currentWeapon].name + ".";

any help will be greatle appreciated :slight_smile:

Post a link to the step or all of the code so it can be checked.

Hello!

This is a theory questions, we don’t need the code.

let currentWeapon = 0
0 is the initial position in the array “weapons”, the stick

When you buy or sell a weapon, you add or subtract the available positions in the weapons array. If you have all four weapons, positions 0 - 3 (remember computers start to count at 0) are available. Though I’m not sure how many weapons you can own here? If it’s only one, the position in the array is your current value.

weapons[currentWeapon] checks the active weapons’ value = uses the weapon object it finds at the given array position.

This is how I understood your question.

I guess where Im confused is how does the let currentWeapon = 0 variable know to pull from the weapons array and not another one?

It doesn’t check itself, it gets checked.
This happens with weapons[currentWeapon value = position of weapon object in array].
That value of currentWeapon starts with 0 and gets updated with every purchase.

2 Likes

Okay its making sense now thank you :slight_smile:

1 Like

I have come across a user that had a similar issue with understanding how fighting and the monsters array work. With valid arguments that the code doesn’t make sense.

We might have a case of “tutorial code” here that looks different on the outside than what happens underneath. Take this with a grain of salt. The good news is that once you are writing “free” code in the wild, your editor will point the mistake out or the app simply stop working.

3 Likes

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