I am trying to make a program that shows the table that the user chooses, but is only compiling the first multiplication. (x * 1 = x)

Asked

Viewed 28 times

-1

var mT = parseInt(prompt("A tabuada de qual numero deseja ver?(preencha somente com o numero.)"));

var n = 1;

function mostra(frase) {
  document.write(frase);
  pL();
};

function pL() {
  mostra("<br>" + "<hr>" + "<br>");
};

while (n <= 10) {
  mostra(mT + " * " + n + " = " + mT * n);
  n = n + 1;
};
<html>

<head>
  <script>
  </script>
</head>

<body bgcolor=000000 text=FF0000>
  <script>
    console.log("apenas para o body :)");
  </script>
</body>

</html>

1 answer

2

Your mistake is here, calling the function pL:

function mostra(frase) {
  document.write(frase);
  pL(); // esse chamada ativa a recursão infinita....
};

That call pL calls again the function mostra and this internally calls the function pL that calls again mostra and so on... and this generates an infinite recursion, which will generate the error:

"Uncaught Rangeerror: Maximum call stack size exceeded"

The simplest way for your case would be to ignore that function pL and work only with mostra, and in the parameters you would pass a <br> and the <hr>

var mT = parseInt(prompt("A tabuada de qual numero deseja ver?(preencha somente com o numero.)"));

var n = 1;

function mostra(frase) {
  document.write(frase);
};

while (n <= 10) {
  mostra(mT + " * " + n + " = " + mT * n + '<br><hr>'); // passa '<br><hr>' aqui para quebrar a linha
  n = n + 1;
};
<html>

<head>
  <script>
  </script>
</head>

<body bgcolor=000000 text=FF0000>
  <script>
    console.log("apenas para o body :)");
  </script>
</body>

</html>


Questions that talk about infinite recursion error:

Browser other questions tagged

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