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

Tell us what’s happening:

Stuck on this step. Imo the wording is not that great, and I’m having a hard time understanding it. Here is the guide text:

Back at the beginning of this project, you created the inventory array. Add the newWeapon to the end of the inventory array using the push() method.

Here is an example:

const arr = ["first"];
const next = "second";
arr.push(next);

arr would now have the value ["first", "second"].

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 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."
  }
];

// 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 fightDragon() {
  console.log("Fighting dragon.");
}

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 fightSlime() {

}

function fightBeast() {

}

Here is the code critical to this step:

function buyWeapon(newWeapon) {
  if (gold >= 30) {
    gold -= 30;
    currentWeapon++;
    goldText.innerText = gold;
    let newWeapon = weapons[currentWeapon].name;
    text.innerText = "You now have a " + newWeapon + ".";
    const inventory = ["stick"];
    const newWeapon = "dagger";
    inventory.push(newWeapon);
  }
}

What am I doing wrong here? Every time I make a fix that the hint section indicates, it just makes a new problem. I’ve tried all sorts of variations of this code in this step, and have arrived here, where the last hint I received was something along the lines of " newWeapon should be added to the inventory array through the push function". I don’t know how accurate the wording is there because after I “fixed” it, no hints appear at all. I have tried everything I can think of and am truly at a loss.

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 90

Hi! Your code is correct, but remove these lines from the function, it was like an example how it works.

And you will pass the task

Thank you so much! Those lines did feel awkward.

1 Like

thank you bro isnt there any details guide ?

Hi! What kind of guide?

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