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

The task say that " Inside the if statement, add the string "Right! You win 20 gold!" to the end of text.innerText . Also, add 20 to the value of gold and update the goldText.innerText ."

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!" 
    gold += 20;
    goldText.innerText = 'Gold: ' + gold;
  }
}

i was able to do the rest but the “goldText.innerText” is not working please does anyone knows why it is not working. Thanks in advance

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

Challenge Information:

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

Hi you just need to call the gold variable after the “+=” assignment

Ex : text.innerText = variable;

variable += 10; do the same result of : variable = variable + 10;

Your code seems correct regarding updating the goldText.innerText. However, if it’s not working as expected, there might be a couple of reasons:

1: Ensure that goldText is correctly referencing the HTML element where you want to display the gold value. Double-check that you’re selecting the element using the correct ID or class, and verify that it exists in your HTML document.

2: If the goldText element is being modified before it’s rendered in the DOM, the changes won’t be reflected. Make sure your JavaScript code is executed after the DOM is fully loaded, either by placing your <script> tag at the end of the HTML body or wrapping your JavaScript code in a DOMContentLoaded event listener.

document.addEventListener(“DOMContentLoaded”, function() {
// Your code here
});
use this line code may be it will work. By addressing these potential issues, you should be able to resolve the problem with updating the goldText.innerText

Hii!

Everything is right with your code, it is just not necessary to put any strings in the goldText.innerText. Also, make sure to check the semicolons!

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