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

Tell us what’s happening:

Hi! I’m stuck on step 119 setting the monsterHealth to be the health of the current monster. This is what I’ve tried so far:

function goFight() {
update(locations[3]);
monstersHealth = monsters[fighting];
}

Your code so far

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

/* file: styles.css */

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

function goFight() {
  update(locations[3]);
  monstersHealth = monsters[fighting];
}

// User Editable Region

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/123.0.0.0 Safari/537.36

Challenge Information:

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

You are assigning an entire object to the monsterHealth variable. If you write this on console;

console.log(monsters[fighting]);

You will receive an object output because monsters is an array that stores objects. However, monstersHealth should be assigned a number variable related to the health of the fighting monster. You can access object properties in two ways:

const sampleObject = {name:"this is sample object", number:41, power:100}
console.log(sampleObject)
/** output
{ name: 'this is sample object', number: 41, power: 100 }
**/
console.log(sampleObject.name)
/** output
this is sample object
**/
console.log(sampleObject["power"])
/** output
100
**/

The lesson wants to add the health value of monsters[fighting] to the monstersHealth variable using dot notation (.). I hope this explanation won’t bore you, because it’s a bit long, but for me, the main goal is always to understand the logic behind this. Happy coding! :slightly_smiling_face:

You have missing health property. Add health using dot notation to your fighting monsters array.

1 Like

I find this explanation really useful and it’s great that you are filling in the gaps of the lesson. I don’t think I’ve applied it properly yet though with:

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

nor this: monsterHealth[monsters[fighting].health];

I’m so going to have a beer tonight after working this one out. :smile:

Your first assign method was right. When you want to assign a value to a variable in JavaScript, you use the = assignment operator. If you use a variable inside parentheses (), it typically means that the variable is being passed as a parameter to a function.

However, monsterHealth is neither a function nor an array -for this monsterHealth[monsters[fighting].health]; answer; it’s simply a variable used to store a value. Arrays only accept index numbers inside square brackets for accessing elements, and objects use property names enclosed in "" to access specific properties. Both arrays and objects are used to square brackets for access existing properties or elements.

So you should assign to fighting monster object’s health prop to monsterHealth variable, how can you do this? :slightly_smiling_face:

Cheers then :beers: :smiley:

Thank you! Just so I can rationise what you’re saying, monsterHealth is a variable (not a function or an array), so the = assignment is correct (so I’m keeping that). I just don’t think I’m understanding what is happening after the =. I’m using dot notation and “”.

monsterHealth = monsters[fighting].“health”;

1 Like

Oh blimey! I’ve just played around with it and it’s passed, but I have no idea how without the “”:

Mod edit: removed solution

You don’t need quotation marks "" when you access object properties with dot notation. However, if you use square brackets to access an object’s property, you should enclose the key in quotation marks. You’re welcome.