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

Tell us what’s happening:

This is my code:

text.innerText += “Right! You win 20 gold!”;
goldText.innerText += 20;

The response is ‘You should use compound assignment to add the string “Right! You win 20 gold!” to the end of text.innerText’

Can someone please tell me where I might be going wrong? Thanking you

Your code so far

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

/* file: styles.css */

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

function pick(guess) {
  const numbers = [];
  while (numbers.length < 10) {
    numbers.push(Math.floor(Math.random() * 11));
  }
  text.innerText = "You picked " + guess + ". Here are the random numbers:\n";
  for (let i = 0; i < 10; i++) {
    text.innerText += numbers[i] + "\n";
  }
  if (numbers.includes(guess)) {
 text.innerText += "Right! You win 20 gold!";
    goldText.innerText += 20; 
}
  }
}

// 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/126.0.0.0 Safari/537.36 Edg/126.0.0.0

Challenge Information:

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

update to be the new value of gold, do not sum 20

Are you able to explain more what you mean? I added it and it did not work.

you need to 1 . update the value of gold, 2. assign the new value of gold to be displayed in goldText.

Post your updated code please

text += “Right! You win 20 gold!”
goldText.innerText = += 20;

not sure i am getting it.

you still need to use innerText

you are not updating the variable that contains a number gold

you are not assigning the new value of the gold variable to be displayed in goldText

Is this it? text.innerText = “Right! You win 20 gold!”

that’s one line, yes
but you need all of them to make the change requested in this step

text.innerText = "Right! You win 20 gold!" + 20;

Am I close?

Here is my updated code:

text.innerText += “Right! You win 20 gold!”;

goldText.innerText = "Gold: " + gold;

gold += 20;

you need first to update gold, then you can update goldText

also goldText is made so that you just need to give it the value of gold, no other text necessary

<span class="stat">Gold: <strong><span id="goldText">50</span></strong></span>

the text Gold: is outside #goldText, you don’t need to assign it when changing the value

I completely lost. I understand about the first line. After that I am lost.

The first one is good. It then asks for 2 things.
Add 20 to the value of gold. “gold” is defined as a variable at the start of the code. You need to add 20 to that.

Then it asks you to update the inner text of goldText. If you look at your code above, goldText links to the index html text. The user interface which has the gold score. You also need to update that . You don’t add 20 manually. In the previous step you have updated the gold so refer to that.

text.innerText += “Right! You win 20 gold!” (gold += 20);
gold += 20;
goldText.innerText = "Gold: " + gold;

Any better?

I meant this code not the last code:

I meant this code:

text.innerText += “Right! You win 20 gold!”;
gold += 20;
goldText.innerText = "Gold: " + gold;

just give it the value of gold, do not add other text

Mod edit: code removed

It is great that you solved the challenge, but instead of posting your full working solution, it is best to stay focused on answering the original poster’s question(s) and help guide them with hints and suggestions to solve their own issues with the challenge.

We are trying to cut back on the number of spoiler solutions found on the forum and instead focus on helping other campers with their questions and definitely not posting full working solutions.

1 Like

Thank you for your invaluable comments!