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

Tell us what’s happening:

I don’t know what’s worng here. I’m getting the value of health for set to monsterHealth

Your code so far

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

/* file: styles.css */

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

function goFight() {
  update(locations[3]);
monsterHealth = monsters[fighting.health]
}

// 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/126.0.0.0 Safari/537.36

Challenge Information:

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

  • monsters is an array of objects.

  • fighting is just a number, you use it as the index into the monsters array to get a single object back.

  • The object you get back has an health property. That is what you set monsterHealth to.

I know the steps what i have to follow but i have done the exercise perfectly and keep dropping an error.

this is the code of monsters arrays:

const monsters = [
  {
    name: "slime",
    level: 2,
    health: 15
  },
  {
    name: "fanged beast",
    level: 8,
    health: 60
  },
  {
    name: "dragon",
    level: 20,
    health: 300
  }
]

Not sure why are you showing me the array?


fighting is a number it does not have a health property on it.

Index into the monsters array using fighting, the object you get back has the health property.


Example code:

const users = [
  { name: "jack", age: 32 },
  { name: "bob", age: 25 },
];

const user = 1;
console.log(users[user].name); // bob

That’s example it’s literally the same i have done in my code idk what’s the diferent.

It’s not quite what you are doing though. fighting is a number, so fighting.health isn’t anything

ok understand now thanks so much.

1 Like

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