Click to See Complete Forum and Search --> : Arrays To Capture Info


tomyknoker
06-21-2003, 03:58 AM
Hey Everyone,
Newbie to this, I want to create a form which will capture horse names and store them in an array, then has a button which you can press for it to output to a new window that picks 3 randomly selected horses from the array. But I want to make sure that a minimum of 8 horses is enetered before the button will work and also that the output in the new window never has the same horse twice,
Thanks everyone again,
Tom

Greelmo
06-21-2003, 12:43 PM
so far i have this, but i'm still working on it... it doesn't open a new window yet, but it does the array action:


<html>
<head>
<title>Horses</title>
<script type="text/javascript">
var horses=new Array();
function fncadd(entry)
{
var mystring=horses.toString();
if (mystring.indexOf(entry)!=-1){
alert("You have already entered that horse!");}
else {
if (horses.length==0){
horses[0]=entry;}
else {
var hello=horses.length;
horses[hello+1]=entry;}
}
}

function fncalert()
{
var dexter=horses.toString();
alert(dexter)
}
</script>
</head>
<body>
<form name="formy">
<input type="text" name="text1" size="15">
<input type="button" value="Add to list" onClick="fncadd(this.form.text1.value);">
<input type="button" value="Hello" onClick="fncalert();">
</form>
</body>
</html>

Greelmo
06-21-2003, 01:24 PM
Here's my next version... only one more kink to work out which will be easy, but i'm about to go swimming. It still will allow 2 horses of the same name to be shown in the random part. My 3rd version will be coming sometime today!


<html>
<head>
<title>Horses</title>
<script type="text/javascript">
var horses=new Array();
function fncadd(entry)
{
var mystring=horses.toString();
if (mystring.indexOf(entry)!=-1){
alert("You have already entered that horse!");}
else {
if (horses.length==0){
horses[0]=entry;}
else {
var hello=horses.length;
horses[hello]=entry;}
}
}

function fncalert()
{
if (horses.length<7){
alert("You need at least 8 entries, and you only have " + horses.length);}
else {
var rand1=Math.random();
var rand2=Math.random();
var rand3=Math.random();
if (rand1>0.7 || rand2>0.7 || rand3>0.7){
fncalert();}
else {
horse1n=Math.round(rand1*10);
horse2n=Math.round(rand2*10);
horse3n=Math.round(rand3*10);
var horse1=horses[horse1n].toString();
var horse2=horses[horse2n].toString();
var horse3=horses[horse3n].toString();
document.getElementById('appears').innerHTML= horse1 + "<br>" + horse2 + "<br>" + horse3;}
}
}
</script>
</head>
<body>
<form name="formy">
<input type="text" name="text1" size="15" onFocus="this.value='';">
<input type="button" value="Add to list" onClick="fncadd(this.form.text1.value);">
<input type="button" value="Submit" onClick="fncalert(); document.formy.text1.focus();">
</form>
<div id="appears"></div>

Greelmo
06-21-2003, 04:38 PM
okay LOL I FINALLY DID IT!!! sorry these entries are so long, but here are your scripts!

It is in 2 separate documents.... the first one is "hello.html" :

<html>
<head>
<title>Horses</title>
<script type="text/javascript">
var horses=new Array();
function fncadd(entry)
{
var mystring=horses.toString();
if (mystring.indexOf(entry)!=-1){
alert("You have already entered that horse!");}
else {
if (horses.length==0){
horses[0]=entry;}
else {
var hello=horses.length;
horses[hello]=entry;}
}
}

function fncalert()
{
if (horses.length<7){
alert("You need at least 8 entries, and you only have " + horses.length);}
else {
var rand1=Math.random();
var rand2=Math.random();
var rand3=Math.random();
if (rand1>0.7 || rand2>0.7 || rand3>0.7){
fncalert();}
else {
horse1n=Math.round(rand1*10);
horse2n=Math.round(rand2*10);
horse3n=Math.round(rand3*10);
var horse1=horses[horse1n].toString();
var horse2=horses[horse2n].toString();
var horse3=horses[horse3n].toString();
window.open("hello2.html", "horsey", "");}
}
}
</script>
</head>
<body>
<form name="formy">
<input type="text" name="text1" size="15" onFocus="this.value='';">
<input type="button" value="Add to list" onClick="fncadd(this.form.text1.value); document.formy.text1.focus();">
<input type="button" value="Submit" onClick="fncalert(); document.formy.text1.focus();">
</form>
<div id="appears"></div>


the next is "hello2.html" :

<html>
<head><title>Horses</title>
<script type="text/javascript">
var horses=window.opener.horses;
function fncwrite()
{
rand1=Math.random();
rand2=Math.random();
rand3=Math.random();
if (rand1>0.7 || rand2>0.7 || rand3>0.7){
fncwrite();}
else {
horse1n=Math.round(rand1*10);
horse2n=Math.round(rand2*10);
horse3n=Math.round(rand3*10);
if (horse1n==horse2n || horse1n==horse3n || horse2n==horse3n){
fncwrite();}
else {
var horse1=horses[horse1n].toString();
var horse2=horses[horse2n].toString();
var horse3=horses[horse3n].toString();
document.write( horse1 + "<br>" + horse2 + "<br>" + horse3);}}
}
</script>

</head>
<body onLoad="fncwrite();">
</body>
</html>


THERE YA GO!!!

tomyknoker
06-21-2003, 07:51 PM
Greeelmo that is perfect thansk heaps :))

Greelmo
06-21-2003, 09:42 PM
my pleasure