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

Hey everyone kindly walk me through this. My code and the instruction are below;

function goFight() {
  update(locations[3]);
  monsterHealth: monsters[fighting];

The instruction is as follows;

Below your update call, set the monsterHealth to be the health of the current monster. You can get this value by accessing the health property of monsters[fighting] with dot notation.

The error message is;
“You should assign the value of monsters[fighting] to the monsterHealth variable.”

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/17.2.1 Safari/605.1.15

Challenge Information:

Hi @Answer_Alyosha

Here is the monsters object.

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

Here is your code:

The instructions are asking you to assign the monsterHealth variable a value.

There are two problems with you code:

  1. it is not assigning anything
  2. it is not accessing the correct key in the object.

If you reference your other functions, you’ll get an idea of what needs to be done.

Happy coding

Hey appreciate your reply. I came up with this.

function goFight() {
  update(locations[3]);
  monsterHealth(300);
}

Hi @Answer_Alyosha

The monsterHealth variable needs to assign a key from the monsters object.

Hard coding a number won’t work, because it will need to change each time the monster receives a hit.

Your first attempt just needs a few fixes.

My brain frying already. Tried this but still

monsterHealth.monsters[fighting] = 300;

I would suggest resetting the lesson.

Let’s take a look at the monsters array again.

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

If I want to access the the current monsters level then I would write this

monsters[fighting].level

For this challenge you need to do something similar but instead of the current monster level it should be the current monster health.

Then once you fix that you need to assign that result to the monsterHealth variable.

remember that assignment works like this

someVariable = something you want to assign to it

hope that helps

Note: your answer should not include any numbers

1 Like

Hey appreciate your insight. However, I’m still confused. kindly guide me.

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

}

What’s the variable and what should I assign it to?
my head aching already.

monsterHealth is the name of the variable.

You don’t need to declare here it

you can reference it by name because it was already declared on line 6 of the project

 monsterHealth

Then lets take a look at this part here

You were almost right.

but you should be referencing health there. not monsterHealth.
reason being is because health is the name of the property in these objects

once you fix that, then assign it to your monsterHealth variable and the test will pass.

your answer should be one line of code

Hey appreciate you taking your time to explicitly walk me through this, but I’m still confused fr.

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

I have to drop off but you are missing two things.

No.1
You are missing the monstersHealth variable in your answer

No.2
Here you have the syntax mixed up

health is supposed to be at the end of that.
not the front.

remember my example from earlier

it is the same syntax but you are going to use health instead

hope that helps

3 Likes

pardon me, I’m somewhat new here and my psyche still getting use to the terminologies, explains why I’m confused.
I won’t stop, gonna keep trying. And Thanks for your patience.

Finally I passed it!!! muchas gracias!

2 Likes

This was very helpful, thank you

hey all she is trying to say is
1- u getting it right but with the wrong tools she used monsters[fighting].level to get the level of the game,

now in this assignment they want u to get the health not the level

so look for health instead of level using same syntax

hope you understand

monsterHealth = monsters[fighting].health;

Here is:

  • monsterHealth - is a function called;
  • monsters.helath - is a data for indicate that with monster is fighting and what’s his health;
  • [fighting] - for indicate monster’s health with which is fighting.

Hi, u do good, but just need to turn to the object and its health field. So u code must look like smth that

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