...

View Full Version : Problem with randomiser script



nicky77
08-08-2007, 02:59 PM
Hi, I'm trying to implement a randomise function for question numbers in a test generator i'm developing. I've picked up a script which I've modified, however, i'm fairly new to javascript and i don't think i've set it up properly. I have a dynamic array of text fields which contain each question number. I want to use onclick to call the randomiser to reorder the questions while making sure they are all unique. I've placed the code in the <head> section, but i get the error that "div has no properties" - even though i am accessing the same <div> for another piece of js validation. Any help would be greatly appreciated.



<script type="text/javascript">
<!-- Begin
/* This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com
Created by: Barry Pranklin :: http://www.pranklin.com */

var Found=false
var div = document.getElementById("preview");
var fields = div.getElementsByTagName("input");
var len = fields.length;
var num = len -1;
var Current = new Array();
var MaxValue=len;
var NumUnique=len ;
var Count=0;
var Current = new Array(NumUnique);
GetUnique();
alert("The unique numbers are: " + Current[0] + ", " + Current[1] + ", and " + Current[2])
function GetUnique()
{
for (i=0;Count<NumUnique;Count++)
{
Found=false
var rndValue = get_random()
var j=0
for (j=0;j<Current.length;j++)
{
if (Current[j] == rndValue)
{
Found=true
break
}
}
if (Found)
{
Count--
} else {
Current[Count]=rndValue
}
}
}
function get_random()
{
var ranNum= Math.round(Math.random()*MaxValue);
return ranNum;
}
// End -->
</script>

calling it here....this may be problem but not sure how to resolve it


<input name="Randomise" value="Randomise question order" onclick="GetUnique(this)" type="submit" title="Submit" />

rwedge
08-08-2007, 08:27 PM
Put the script below the form in the HTML, maybe just before the closing body tag.

It will fail in the head, or before the form, because the script is looking for the id 'preview'
( most likely your form id ) to get the input elements length:

var div = document.getElementById("preview");
var fields = div.getElementsByTagName("input");If the form is not loaded before the script, the attempt to set the variable 'div' will fail, because it does not exist yet.

mcjwb
08-08-2007, 08:28 PM
You are trying to access the div before it has been created. You need to have a function that initialises the variables after the page has loaded.
Try this:

<html>
<head>
<title> New Document </title>
<script type="text/javascript">
<!-- Begin
/* This script and many more are available free online at
The JavaScript Source!! http://javascript.internet.com
Created by: Barry Pranklin :: http://www.pranklin.com */
var Found=false
var div;
var fields;
var len;
var num;
var Current = new Array();
var MaxValue;
var NumUnique;
var Count=0;
var Current = new Array();
function generateRands(){
div = document.getElementById("preview");
fields = div.getElementsByTagName("input");
len = fields.length;
num = len -1;
MaxValue=len;
NumUnique=len ;
Current = new Array(NumUnique);
GetUnique();
alert("The unique numbers are: " + Current[0] + ", " + Current[1] + ", and " + Current[2])
}
function GetUnique()
{
for (Count=0;Count<NumUnique;Count++)
{
Found=false;
var rndValue = get_random();
var j;
for (j=0;j<Current.length;j++)
{
if (Current[j] == rndValue)
{
Found=true
break
}
}
if (Found)
{
Count--
} else {
Current[Count]=rndValue
}
}
}
function get_random()
{
return ranNum= Math.round(Math.random()*MaxValue);
}
// End -->
</script>
</head>
<body onload="generateRands()">
<input name="Randomise" value="Randomise question order" onclick="generateRands()" type="submit" title="Submit" />

<div id="preview">
<input type="text" name="test1"><br>
<input type="text" name="test2"><br>
<input type="text" name="test3">
</div>
</body>
</html>
Also your call to GetUnique() in the submit button doesn't accept any variables so loose the "this". Calling GetUnique will generate new numbers each time but the function just assigns the numbers to a variable so you won't see any changes.

nicky77
08-09-2007, 02:47 PM
Many thanks for your replies - I've taken your advice and implemented the following script which works perfectly.



<script type="text/javascript">

var Found=false
var div;
var fields;
var len;
var num;
var Current = new Array();
var MaxValue;
var NumUnique;
var Count=0;
var Current = new Array();
function generateRands(){
div = document.getElementById("preview");
fields = div.getElementsByTagName("input");
len = fields.length;
num = len -1;
MaxValue=len-1;
NumUnique=len ;
Current = new Array(NumUnique);
GetUnique();
//loop through fields, updating with randomly generated number
for(var i=0;i<len;i++)
{
fields[i].value=Current[i];
}
}
function GetUnique()
{
for (Count=0;Count<NumUnique;Count++)
{
Found=false;
var rndValue = get_random();
var j;
for (j=0;j<Current.length;j++)
{
if (Current[j] == rndValue)
{
Found=true
break
}
}
if (Found)
{
Count--
} else {
Current[Count]=rndValue
}
}
}
function get_random()
{
return ranNum= Math.round(Math.random()*MaxValue+1);
}
</script>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum