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

Tell us what’s happening:

I am not doing as I’ve been asked to add the new value in the array on inventory with the push method. I’ll really appreciate your help with that If possible.

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 buyWeapon() {
  if (gold >= 30) {
    gold -= 30;
    currentWeapon++;
    goldText.innerText = gold;
    let newWeapon = weapons[currentWeapon].name;
    text.innerText = "You now have a " + newWeapon + ".";
    inventory.push("dagger");
  }
}

function fightSlime() {

}

function fightBeast() {

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

/* file: styles.css */

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

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

// User Editable Region

Your browser information:

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) 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 90

Read the error message more carefully here

You should push the value of newWeapon to the inventory array.

right now you are pushing "dagger" which is incorrect

you should not hardcode a value

you should use the variable they say to in the error message

then the test will pass

hope that helps

1 Like

Let me try. Just one thing can you tell me what should i be writing here as

inventory.push( );
I mean in between the paranthesis. This is my first ever learning module and I got a bit confused about the whole code as it’s kept on incrementing

You’re issue is this part specifically

but the directions say to push the value of newWeapon

once you fix that issue, then it will pass

Another way to think about it is this way.

The goal of the buy weapon function is to buy a weapon and add that new weapon to your inventory.

It would make sense to add that new weapon to your inventory.

But it wouldn’t make sense to add a "dagger" each time would it?

What if the player didn’t buy a "dagger" ?
What if they bought a sword or something else?

You want to be able to dynamically add that new weapon and account for whatever the player buys.

Not hardcode a value like "dagger" because the player might not buy that

hope that makes sense

2 Likes

Got it. Thanks a lot

1 Like

image

I am stuck here: Step 39

Comments allow you to add notes to your code. In JavaScript, single-line comments can be written with // and multi-line comments can be written with /* and */ . For example, here are single and multi-line comments that say “Hello World”:

// hello world


 

/*

hello world

*/


Add a single-line comment that says initialize buttons .

How do I Add a single-line comment that says initialize buttons to my code? I am working on the “Role playing Game.” I am in step 39. Thanks

// initialize buttons
you can write your code here instead of my sentence in this very line. Hope it helps

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