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

Tell us what’s happening:

Hi all, I am not sure what I am doing wrong here. The challenge asks :

Up until now, any time text.innerText was updated, the old text was erased. This time, use the += operator to add text to the end of text.innerText.

Add the string In your inventory you have: - include the spaces at the beginning and the end.

Your code so far

function buyWeapon() {
  if (gold >= 30) {
    gold -= 30;
    currentWeapon++;
    goldText.innerText = gold;
    let newWeapon = weapons[currentWeapon].name;
    text.innerText += " " + 'In your inventory you have: ' +" ";
    inventory.push(newWeapon);
    
  }
}

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 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/120.0.0.0 Safari/537.36

Challenge Information:

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

Hi! Include the spaces directly in your string without using +

Hi Daniel,

Like this? It’s not working still :thinking:

function buyWeapon() {
  if (gold >= 30) {
    gold -= 30;
    currentWeapon++;
    goldText.innerText = gold;
    let newWeapon = weapons[currentWeapon].name;
    text.innerText +=" In your inventory you have: ";
    inventory.push(newWeapon);
    
  }
}

It seems like You’re asked to use the += operator to append text to the existing innerText rather than replacing it entirely. Here’s the corrected version of your function:
function buyWeapon() {
if (gold >= 30) {
gold -= 30;
currentWeapon++;
goldText.innerText = gold;
let newWeapon = weapons[currentWeapon].name;
text.innerText += " In your inventory you have: ";
inventory.push(newWeapon);
}
}
i hope it will work for you

Best Regard
Danish Hafeez | QA Assistant
ictinnovations.com

Thank you , unfortunately it hasn’t worked

function buyWeapon() {
if (gold >= 30) {
gold -= 30;
currentWeapon++;
goldText.innerText = gold;
let newWeapon = weapons[currentWeapon].name;
text.innerText += " In your inventory you have: ";
inventory.push(newWeapon);

}
}

Double-check that the innerText property of the text element is correctly updated after the function is called. Make sure there are no conflicting operations that might overwrite the text.

Hi @k-v-g
Your code is missing the original text.innerText = line.

Happy coding

Ah thank you for clearing this up!
Here is the correct code that passes:

function buyWeapon() {
if (gold >= 30) {
gold -= 30;
currentWeapon++;
goldText.innerText = gold;
let newWeapon = weapons[currentWeapon].name;
text.innerText = "You now have a " + newWeapon + “.”;
text.innerText += " In your inventory you have: ";
inventory.push(newWeapon);

}
}

1 Like