Starting with Javascript

Asked

Viewed 37 times

0

I apologize if I’m posting in the wrong place The point is that I’m starting in Javascript and I don’t know why this script doesn’t work.

<body>
  <p>Ganhe alguns meticais</p>
  <h2 id="cash">0</h2>
  <button onclick="jogar();">Jogar</button>
</body>

<script>
  function jogar() {
    var x = 1;
    while (x < 1000) {
      document.getElementById('cash').innerHTML = String(x);
      x = x + 10;
      if (x === 150) {
        break ();
      }
    }
  }
</script>

  • 3

    the problem is that break(); is a command, not a Function, so you need to remove the parentheses: break;

  • Thanks, that’s what it was. But still the script only prints one number, besides updating #cash when the variable x came incrementing. Could give me a light?

  • your script should run so fast that you can’t notice the values changing

  • Thanks. I’ll figure it out.

  • You can set a 1 or 2 second wait for each number change x = x + 10;. Can use setInterval or setTimeout() for that reason.

1 answer

1

Hello, it’s a matter of understanding how to find the error; if you use the Chrome browser, for example, you can Inspect Element by pressing Ctrl+Shift+I and access the Console tab to find occasional problems, especially javascript.

In your code for example, if you go to Inspect > Console, we see this message: inserir a descrição da imagem aqui

The message says "Not expected ("; that is, break does not expect attributes from a function. If you click on the code, it signals exactly where the problem is inserir a descrição da imagem aqui

The answer to whether a JS script works or not is this; good luck!

Browser other questions tagged

You are not signed in. Login or sign up in order to post.