0

I am trying to write a simple random number generator, where you input 3 integers into forms. The program then returns a certain amount of random numbers between the other two values. When I open the page the forms are displayed but when I click the button to generate the random numbers nothing happens. Why is this happening?

<html>
<head>
<script LANGUAGE="JavaScript">


    function randomFromTo(from, to)
    {
        return Math.floor(Math.random() * ((to - from) + 1) + from);
    }
function include(arr, obj)
{
    for(var j = 0; j < arr.length; j++)
    {
    if (arr[j] == obj) return true;
    }
}
    function RandomGen(form)
    {
    var enteries = new Array();
    var number = form.from.value;
    var top = form.top.value;
    var size = form.inputBox.value;
    var count;
    for (count = 0; count < size; count++)
    {
        var num = randomFromTo(number, top);
        if (include(enteries, num) == true)
        {
            count--;
        }
        else
        {
            enteries[count] = num;
        }
    }
    var i;
    for(i = 0; i <= enteries.length; i++)
    {
        document.write(enteries[i]);
        document.write("<br>");
    }
}


</script>
</head>

<body>

<center><h1>Random Number Generator</h1></center>
<form name="myform" action="" method="GET">Enter the Range of Values
<input type="text" name = "from" value="">to
<input type="text" name = "top" value="">
<p>Enter The Amount of Random Numbers Needed
<input type="text" name = "inputBox" value=""><p>
<input type="button" name="button" value="Generate" onClick=RandomGen(this.form)">
</form>
</body>

2
  • first change your script tag to <script type="text/javascript"> Commented Aug 11, 2011 at 21:28
  • 1
    @Timster onClick=RandomGen(this.form)" missing ". Commented Aug 11, 2011 at 21:29

4 Answers 4

1

You have a syntax error here

<input type="button" name="button" value="Generate" onClick=RandomGen(this.form)">

should be

<input type="button" name="button" value="Generate" onClick="RandomGen(this.form)">

Also this part should be updated, not because it will cause an error but because it is 2011

<script LANGUAGE="JavaScript">

should be

<script type="text/javascript">

Update

Or:

<script>

no need for the type attribute because it is 2018!

Sign up to request clarification or add additional context in comments.

Comments

0

You missed a quote:

<input type="button" name="button" value="Generate" onClick="RandomGen(this.form)">

Comments

0
onclick="RandomGen(this.form)"

That part of your code was malformed. onclick is always all lower case and you were missing a "

Comments

0

Your missing a "

    <input type="button" name="button" value="Generate" onClick="RandomGen(this.form)">

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.