How to show a vector without going to another page

Asked

Viewed 68 times

0

Well I’m making a page that makes draws, I already managed to do half the work, but when I show the result of the vector it goes to another page, I wanted to know how to show what has in the vector in the same page.

My html

 <!DOCTYPE html>
 <html>
  <head>
    <title>Gerador</title>
    <link rel="stylesheet" type="text/css" href="estilo.css">
    <script src="interacao.js"></script>
    <meta charset="utf-8">
 </head>
<body>
   <h1 id="principal">Gerador de apostas</h1>
 <hgroup id="menu">
    <a href="file:///D:/Programa%C3%A7%C3%A3o/Gerador%20de%20apostas/index.html"><p>Inicio</p></a>
    <a href="file:///D:/Programa%C3%A7%C3%A3o/Gerador%20de%20apostas/gerador.html"><p>Gerador</p></a>
    <a href="file:///D:/Programa%C3%A7%C3%A3o/Gerador%20de%20apostas/sobre.html"><p>Sobre</p></a>
</hgroup>
<hgroup id="apostas">
    <button onclick="mega()">Mega-Sena</button>
    <button onclick="lotofacil()">Quina</button>
    <button onclick="quina()">Lotomania</button>
    <button onclick="lotomania()">LotoFácil</button>
</hgroup>

My javascript

function mega(void) {

   var numeros = [];
   var numero,verifica;
   for(var i=0;i<6;i++)
   {
      do
      {
           numero = Math.floor(Math.random()*61);
           verifica = false;
           for(var j=0; j < i;j++)
           {
              if (numeros[j]==numero) {
                verifica=true;
              }
            }

       }while(verifica);
       numeros.push(numero);
    }
    for(var i=0;i<6;i++)
    {
       document.write(numeros[i]);
    }

}

  • My answer doesn’t solve the problem?

  • Thank you, I had forgotten to dial as solved

1 answer

1


There are many ways to do this, it is possible to create elements on the same page and it is also possible to update element information on the page, which is the most common to do. I thought it would be nicer to fill in the input’s with the generated values:

function mega() {
  var mega = document.getElementsByClassName('mega'); 
  var numeros = [];
  var numero,verifica;
  for(var i=0;i<6;i++)
  {
    do
    {
      numero = Math.floor(Math.random()*61);
      verifica = false;
      for(var j=0; j < i;j++)
      {
        if (numeros[j]==numero) {
          verifica=true;
        }
      }
    }while(verifica);
    numeros.push(numero);
  }
  for(var i=0;i<6;i++)
  {
    mega[i].value=numeros[i];
  }
}
<button onclick="mega()">Mega-Sena</button>
<div>
  <input class="mega" type="text" style="width: 40px; text-align: center;" readonly>
  <input class="mega" type="text" style="width: 40px; text-align: center;" readonly>
  <input class="mega" type="text" style="width: 40px; text-align: center;" readonly>
  <input class="mega" type="text" style="width: 40px; text-align: center;" readonly>
  <input class="mega" type="text" style="width: 40px; text-align: center;" readonly>
  <input class="mega" type="text" style="width: 40px; text-align: center;" readonly>
</div>

MEGA-SENA numbers go from 1 to 60, its function can assign zero in the combination, so the line that generates the numbers should be: numero = Math.floor((Math.random() * 60) + 1); Behold: https://www.w3schools.com/jsref/jsref_random.asp

Browser other questions tagged

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