Guys, please i need your help on this.
Your code so far
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 104
ILM
February 8, 2024, 2:55pm
2
what kind of help do you need? what code have you written?
1 Like
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 (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 = " . "
}
}
function fightSlime() {
}
function fightBeast() {
}
ILM
February 8, 2024, 2:58pm
5
what’s your question about this code?
Step 104
After your currentWeapon
, use the concatenation operator to set text.innerText
to the string "You sold a "
, then currentWeapon
, then the string "."
.
ILM
February 8, 2024, 2:58pm
7
you should do it all in one line instead of two
i tried that, but still not getting it please.
Physayo
February 8, 2024, 3:06pm
10
it says I sent it earlier and can’t resend it, can you please check my previous reply for it?
ILM
February 8, 2024, 3:06pm
11
you said you changed your code and it wasn’t working, not that you tried with the same exact code, show your new code please
Physayo
February 8, 2024, 3:10pm
12
function sellWeapon() {
if (inventory.length > 1) {
gold += 15;
goldText.innerText = gold;
let currentWeapon = inventory.shift();
text.innerText = "You sold a "+ currentWeapon+"","+ .+";
}
}
Physayo
February 8, 2024, 3:10pm
13
i guess i’m not doing it right.
ILM
February 8, 2024, 3:12pm
14
use less concatenation operators, only 2, at the end you need to only concatenate a period
Physayo
February 8, 2024, 3:16pm
15
I don’t really understand that, please.
ILM
February 8, 2024, 3:16pm
16
what do you not understand? you only need one concat operator before the variable and one after, please show what you tried
Physayo
February 8, 2024, 3:19pm
17
function sellWeapon() {
if (inventory.length > 1) {
gold += 15;
goldText.innerText = gold;
let currentWeapon = inventory.shift();
text.innerText = "You sold a "+ currentWeapon "." +""
}
ILM
February 8, 2024, 3:19pm
18
Physayo:
currentWeapon "."
this is not valid, you need to have a concat operator in there
Physayo:
+""
concatenating an empty string does nothing, you can avoid that
1 Like
Physayo
February 8, 2024, 3:21pm
19
Got it now, thanks for your time.
1 Like
system
Closed
August 9, 2024, 3:21am
20
This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.