Need help understanding the logic behind this?

I have already finished the “learn basic javascript by building a role playing game” section. And in fact, I’ve actually gone through it twice now. But although I have finished all of the steps, I am having a really, really difficult time understanding why exactly, say for example in step 116, I put [fighting] on monsters[fighting] when fighting is not part of any array? Why do I have to have it there, isn’t that just a variable I declared?

Maybe my brain has melted a little bit and I’m not thinking straight/or this is a stupid question, but I don’t get it. Can someone explain, in very “tell me like I’m 5” explanations why this is done? For some reason this is the only part of this lesson that is confusing me.

2 Likes

The first step to answering this question is to understand what each of those variables represent, in other words, what values they hold. So what does the variable monsters hold? What does the variable fighting hold?

1 Like

I think const monsters holds the objects of the name, level, and health of each of the monsters. As for fighting, other than being declared at the top of let = fighting, I see that it is also used in the functions for fightSlime, fightBeast, fightDragon, with 0 , 1, and 2.(At the point of step 116) Am I on the right track?

Yes, monsters holds the individual monster objects. But I think we need to be a little more specific. It is an “array” of monster objects. So how do you access an item in an array? Based on that, what do you think the variable fighting will be? And not just its literal value. What is that variable being used to track? It’s name should give it away :slightly_smiling_face:

1 Like

You access it with , I believe, so is fighting supposed to represent the monster you are fighting? By the number it uses? Is that the logic to it?

1 Like

Yes, you use brackets to access an element in an array, but what goes inside of those brackets? There are three possible monsters to fight. What do you think the variable fighting is used for?

Ahh, sorry, I just realized that you answered this question in your previous post, I just didn’t read it as well as I should have.

Yes, fighting keeps track of the monster you are currently fighting.

So now that you know what monsters and fighting represent, what does monsters[fighting] get you?

2 Likes

so happy i read this! for future learners (and correct me if I’m wrong), “fighting” and “monsters” are compliments of each other. By giving “fighting” values of 0-2 in the respective functions, you are allowing “fighting” to help identify the monsters you are dealing with (i.e monsters[fighting] in fightSlime breaks down to “fighting”=0 thus you are essentially typing monster[0])

again correct me if my explanation is wrong, but i hope this helps future learners.

edit: to further this understanding/theory, would this mean that location[fighting] within the fightSlime function would call the “town square” object (the first or 0 of the “location” array)? love to hear the thoughts of someone with more experience.