How to show a vector without going to another page

0

Well I'm doing a page that makes sweepstakes, 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 be able to show what's in the vector on 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]);
    }

}

    
asked by anonymous 27.10.2018 / 15:07

1 answer

1

There are many ways to do this, you can create elements on the same page and you can also update element information on the page, which is the most common thing to do. I found it cooler by filling 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>
  

The MEGA-SENA numbers range from 1 to 60, your function can assign zero in the combination, so the line that generates the numbers should be: numero = Math.floor((Math.random() * 60) + 1);   See: link

    
27.10.2018 / 15:37