Button does not run Javascript

0

I'm having a hard time making the button responsible for generating the alert with the lucky number to start the javascript, I'm using the code below for the javascript:

    <script type="text/javascript">


    $('#btnGnrt').click(function() {

    function sorteio_numero(nummin, nummax, numqtd){
        var amostra   = new Array(),
        lista     = new Array(),
        tamanho   = 0,
        aleatorio = 0;
        for(var i = nummin; i <= nummax; i++){
            amostra.push(i);
        }
        while( lista.length < numqtd ){

            tamanho = amostra.length;


            aleatorio = Math.floor( Math.random() * tamanho );


            lista.push( amostra.splice(aleatorio,1) ); 
        }
        return lista;
    }

    alert(sorteio_numero(1000,9999,1));

</script>

and the following to the button:

<div class="button">
    <br><button id="btnGnrt">Gerar Sorteio</button><br>
</div>

The page contains only the button and a header with the title, I can not see the reason for the "not working" button

    
asked by anonymous 30.07.2018 / 21:18

2 answers

2

There are several errors in your code:

  • You did not properly close this block $('#btnGnrt').click(function() {

  • tamanho = 0, should end a line with ;

  • tamanho = amostra.length; there is no need to be within the while being assigned to each interaction.
  • Note: I have modified your code for js pure.

    Running

    var button = document.querySelector('button');
    button.addEventListener('click', function () {
        console.log(sorteio_numero(1000, 9999, 1));
    })
    
    function sorteio_numero(nummin, nummax, numqtd) {
        let amostra = [];
        let lista = [];
        let tamanho = 0;
        let aleatorio = 0;
        
        //Adiciona todos os números entre um Rang
        for (let i = nummin; i <= nummax; i++) {
            amostra.push(i);
        }
        
        tamanho = amostra.length;
        
        while (lista.length < numqtd) {
          aleatorio = Math.floor(Math.random() * tamanho);
          lista.push(amostra.splice(aleatorio, 1));
        }
        return lista;
    }
    <div class="button">
        <br><button id="btnGnrt">Gerar Sorteio</button><br>
    </div>
        
    30.07.2018 / 21:33
    2

    I'm missing the structure of click , which I commented below:

    $('#btnGnrt').click(function() {
        function sorteio_numero(nummin, nummax, numqtd) {
            var amostra = new Array(),
                lista = new Array(),
                tamanho = 0,
                aleatorio = 0;
            for (var i = nummin; i <= nummax; i++) {
                amostra.push(i);
            }
            while (lista.length < numqtd) {
                tamanho = amostra.length;
                aleatorio = Math.floor(Math.random() * tamanho);
                lista.push(amostra.splice(aleatorio, 1));
            }
            return lista;
        }
    
        alert(sorteio_numero(1000, 9999, 1));
    });  // essa estrutura aqui 
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script><divclass="button">
        <br><button id="btnGnrt">Gerar Sorteio</button><br>
    </div>
        
    30.07.2018 / 21:31