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

Tell us what’s happening:

Your code so far

WARNING

The challenge seed code and/or your solution exceeded the maximum length we can port over from the challenge.

You will need to take an additional step here so the code you wrote presents in an easy to read format.

Please copy/paste all the editor code showing in the challenge from where you just linked.

Replace these two sentences with your copied code.
Please leave the ``` line above and the ``` line below,
because they allow your code to properly format in the post.

Your browser information:

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

Challenge Information:

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

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

I don’t understand what current weapon the player has,

Hi @masha2

You’ll need to first fix the errors regarding the second text.innerText.
Then the spacing for the second string.

When that is done, the error message will display the variable needed to display the current weapon.

Happy coding

It would be great to understand how to do this and what to do

text.innerText += " You attack it with your " + weapons[currentWeapon].name + "."

If we move on to the array of weapons, then what does the current weapon have to do with it? I thought if we set the name of the array, then we can only enter inside this array, but it’s not at all clear how this works.

Hi @masha2

I’m glad you figured out how to solve the step.

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

The variable weapons is a reference to an array.
It contains all the weapons in the game.

weapons[currentWeapon].name

currentWeapon is a number, which is used to index the weapons array.
Inside the array, each item is an object.

Dot notation is used to find the name of the item.

For example:
if currentWeapon is 0.
then weapons[0] would access the object { name: 'stick', power: 5 }

then, weapons[0].name would access the string stick.

Happy coding

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