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

Tell us what’s happening:

I’ve been trying to understand this step for the past hour. Can someone tell me how does monsterHealth relate to Health and monsters[fighting]
I do see the let monsterHealth variable at line 6. Its my understanding that we are trying to assign that variable a value by having it equal to be the health of the current monster, and I see the const monsters at line 25, so that is either slime, fanged beast, or dragon.

but how does [fighting] relate to this? The first reference in my code using this word is line 140. But why is it does it have to be in square brackets and what does that do with it being placed right next to monsters just like this - monsters[fighting].

from how I understand it, it should be monsterHealth = monsters.health;
but this answer is wrong

Your code so far

let xp = 0;
let health = 100;
let gold = 50;
let currentWeapon = 0;
let fighting;
let monsterHealth;
let inventory = ["stick"];

const button1 = document.querySelector('#button1');
const button2 = document.querySelector("#button2");
const button3 = document.querySelector("#button3");
const text = document.querySelector("#text");
const xpText = document.querySelector("#xpText");
const healthText = document.querySelector("#healthText");
const goldText = document.querySelector("#goldText");
const monsterStats = document.querySelector("#monsterStats");
const monsterName = document.querySelector("#monsterName");
const monsterHealthText = document.querySelector("#monsterHealth");
const weapons = [
  { name: 'stick', power: 5 },
  { name: 'dagger', power: 30 },
  { name: 'claw hammer', power: 50 },
  { name: 'sword', power: 100 }
];
const monsters = [
  {
    name: "slime",
    level: 2,
    health: 15
  },
  {
    name: "fanged beast",
    level: 8,
    health: 60
  },
  {
    name: "dragon",
    level: 20,
    health: 300
  }
]
const locations = [
  {
    name: "town square",
    "button text": ["Go to store", "Go to cave", "Fight dragon"],
    "button functions": [goStore, goCave, fightDragon],
    text: "You are in the town square. You see a sign that says \"Store\"."
  },
  {
    name: "store",
    "button text": ["Buy 10 health (10 gold)", "Buy weapon (30 gold)", "Go to town square"],
    "button functions": [buyHealth, buyWeapon, goTown],
    text: "You enter the store."
  },
  {
    name: "cave",
    "button text": ["Fight slime", "Fight fanged beast", "Go to town square"],
    "button functions": [fightSlime, fightBeast, goTown],
    text: "You enter the cave. You see some monsters."
  },
  {
    name: "fight",
    "button text": ["Attack", "Dodge", "Run"],
    "button functions": [attack, dodge, goTown],
    text: "You are fighting a monster."
  }
];

// initialize buttons
button1.onclick = goStore;
button2.onclick = goCave;
button3.onclick = fightDragon;

function update(location) {
  button1.innerText = location["button text"][0];
  button2.innerText = location["button text"][1];
  button3.innerText = location["button text"][2];
  button1.onclick = location["button functions"][0];
  button2.onclick = location["button functions"][1];
  button3.onclick = location["button functions"][2];
  text.innerText = location.text;
}

function goTown() {
  update(locations[0]);
}

function goStore() {
  update(locations[1]);
}

function goCave() {
  update(locations[2]);
}

function buyHealth() {
  if (gold >= 10) {
    gold -= 10;
    health += 10;
    goldText.innerText = gold;
    healthText.innerText = health;
  } else {
    text.innerText = "You do not have enough gold to buy health.";
  }
}

function buyWeapon() {
  if (currentWeapon < weapons.length - 1) {
    if (gold >= 30) {
      gold -= 30;
      currentWeapon++;
      goldText.innerText = gold;
      let newWeapon = weapons[currentWeapon].name;
      text.innerText = "You now have a " + newWeapon + ".";
      inventory.push(newWeapon);
      text.innerText += " In your inventory you have: " + inventory;
    } else {
      text.innerText = "You do not have enough gold to buy a weapon.";
    }
  } else {
    text.innerText = "You already have the most powerful weapon!";
    button2.innerText = "Sell weapon for 15 gold";
    button2.onclick = sellWeapon;
  }
}

function sellWeapon() {
  if (inventory.length > 1) {
    gold += 15;
    goldText.innerText = gold;
    let currentWeapon = inventory.shift();
    text.innerText = "You sold a " + currentWeapon + ".";
    text.innerText += " In your inventory you have: " + inventory;
  } else {
    text.innerText = "Don't sell your only weapon!";
  }
}

function fightSlime() {
  fighting = 0;
  goFight();
}

function fightBeast() {
  fighting = 1;
  goFight();
}
function fightDragon() {
  fighting = 2;
  goFight();
}

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

Challenge Information:

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

I’ve edited your code for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (').

Hi @raven1177 !

Welcome to the forum!

To better understand this, you need to look at the monsters array

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

to access the first object in the array, you would write monsters[0]

 {
    name: "slime",
    level: 2,
    health: 15
  },

to access the second object in the array, you would write monsters[1]

 {
    name: "fanged beast",
    level: 8,
    health: 60
  },

and then to access the third object you would write monsters[2]

  {
    name: "dragon",
    level: 20,
    health: 300
  }

to access a property from one of those objects, you will need to use a combination of bracket and dot notation.

For example, let’s say you wanted to access the name of the first monster here

const monsters = [
  {
    name: "slime",
    level: 2,
    health: 15
  },

you would need to reference the monsters array followed by the index number of the element you want to access, followed by the name

monsters[0].name

the following code would be the "slime" monster.

if you wanted to access the first monsters level, then you would reference the monsters array followed by the index number of the element you want to access, followed by the level

monsters[0].level

The fighting variable represents an index number. It will either be the number 0, 1, or 2.
0 is the first monster object, 1 is the second monster object, and 2 would point to the third monster object.

The monsterHealth variable gets the health of the current monster you are fighting.

Hope that helps

1 Like

Hello everyone,

The real problem is that variable fighting is not defined and as we look in to code we assume that we need to use something defined and with value, I have passed some other courses and I remember, but for someone who is doing it for first time it is very difficult just to assume it will be defined as game is built so it is not options to use.

So for those who want to understand, variable fighting will have index of monster that we fight later so use it as index of array monsters.

@agnerage I will repeat this so it is very clear. Do not post solution code.

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