-3

And how would I keep a list of the last 5 numbers generated? How should I apply this function in the following code?

<button onclick="document.body.innerHTML += (Math.floor(Math.random() * ( 989000000 - 988000000 ) + 987000000 ) + '<br>' );">Gerar Números</button><br>

1 Answers1

0

if you want to use only js I found this to create a file with js then I edited the script for your purpose, see the result:

    let i=0;
    let offset=-5;
    let values="";
    let $list = $("#list");

    function generateRandom() {

        var ran = Math.floor(Math.random() * ( 989000000 - 988000000 ) + 987000000 );
        //textArea.value += ran + "\n";
        values += ran +"\n";

        $list.prepend('<li id="' + i++ + '">' + ran + '</li>');

        if(offset>=0){
            $("#" + offset).remove();
        }
        offset++;

    }

    (function () {
        var textFile = null,
            makeTextFile = function (text) {
                var data = new Blob([text], {type: 'text/plain'});

                // If we are replacing a previously generated file we need to
                // manually revoke the object URL to avoid memory leaks.
                if (textFile !== null) {
                    window.URL.revokeObjectURL(textFile);
                }

                textFile = window.URL.createObjectURL(data);

                return textFile;
            };


        var create = document.getElementById('create');

        create.addEventListener('click', function () {
            var link = document.getElementById('downloadlink');
            link.href = makeTextFile(values);
            link.style.display = 'block';
        }, false);
    })();
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<button onclick="generateRandom()">Gerar Números</button>
<ul id="list"></ul>
<button id="create">Create file</button>
<a download="info.txt" id="downloadlink" style="display: none">Download</a>
giovybus
  • 349
  • 1
  • 3
  • 10
  • Thank you so much! This is exactly what I was needing. It may not be possible to save the information of the last 5 numbers generated in the html but I thank you very much for your help. Your help was perfect. – Renato Costa Apr 26 '20 at 21:53