seanomomom seanomomom - 4 months ago 12
CSS Question

How to set a randomly a generated number for a simple guess the number game in HTML 5 and javascript

I'm coding a simple game using JavaScript, CSS and HTML5. and i need some help to simply get a generated number

Answer Source

Your problem is that the variable 'my_no' is undefined because the function 'rnd' does not return anything. Assuming that you need to generate a number between 'From' and 'To' (inclusive), you should change your 'rnd' function like this:

function rnd(from, to) 
{
    /* Returns a random number between from and to (inclusive) */

    return Math.floor((Math.random() * (to - from + 1))) + from;
}

After that change, the range function should look like this:

function range() 
{
    var to = parseInt(document.game.to.value);
    var from = parseInt(document.game.from.value);

    count = 0;

    my_no = rnd(from, to);
    document.game.help.value = "Guess a number, enter it, and press Guess.";
}

Have fun unlocking the sphinx ;)