dimanche 14 octobre 2018

JavaScript: random num function always gets the same output

i have the following code:

    var lives = 10;
    var score = 0;
    var input = $('#input');
    var board = $('#board');
    var validate = $('#validate');


    function randomNum(min, max) {
        return Math.random() * (max - min) + min;
    }

    var levelOne = (randomNum(0, 999));
    var levelTwo = (randomNum(999, 1999));

    $('#generate').click(function () {
        if (score > 1) {
            board.html(levelOne);
            console.log(levelOne)
            setTimeout(function () {
                board.fadeOut();
            }, 3000);
        } else {
            board.html(levelTwo);
            console.log(levelTwo)
            setTimeout(function () {
                board.fadeOut();
            }, 3000);

        }



    })

The first number gets it output as expected, but if I keep on generating numbers, the log's say it's the same number plus I can't see it in the screen (the timeout is not working as well?). I've done this game in Objective-C but now, i JS, something is missing in my logic. Can someone give me a hint?




Aucun commentaire:

Enregistrer un commentaire