Click to See Complete Forum and Search --> : Wanted: JavaScript Guru - Recursion problem/error


vincentms
04-17-2003, 05:44 AM
Hello All,

I have made a lot of progress with JavaScript in the last week. But, I have hit a wall now!!!

Please look at my script and tell me why I am getting this little error. I want to press the next button repeatedly and not get errors. Must I do some sort of clear/reset each time??? At some point I will add some logic (i.e., if x = 20 THEN (Press FINISH Button) execute an allFinished Function.

Thanks,
Vincent

Zürich, Switzerland
Go Raiders





<HTML>
<HEAD>
<SCRIPT>
var S = 0;
var p = 1;
var tpq = 5;

function getRandom() {
s = Math.round((p - 0.5) + (((tpq + 0.49999) - (p- 0.5)) * Math.random()));
getNext();
return r;
}

function getNext() {
document.write('<p><input type="radio" value="a" name="q10"> Senegal<br></p>') ;
document.write('<p><input type="radio" value="b" name="q10">' + " " + s + '</span></<br></p>') ;
document.write('<p><input type="radio" value="c" name="q10"> Nigeria<br></p>') ;
document.write('<p><input type="radio" value="d" name="q10"> Iraq<br></p>') ;
document.write('<INPUT type="button" value=" Next" onClick=" getRandom()"> ');
return r;
}

</SCRIPT>
</HEAD>

<BODY>
<FORM name="QForm">
<INPUT type="button" value="Start" onClick=" getRandom()">
</FORM>
</BODY>
</HTML>

khalidali63
04-17-2003, 05:54 AM
You should have mentioned that whats the error or what are you trying to do when you press next...is it supposed to be printed over and over ?
If so then you will need to change your logic.
You can use a div element on the page and use innerHTML to write the html over and over.

Khalid

AdamGundry
04-17-2003, 05:59 AM
There are two things you need to do:
1. Remove the "r" after each "return" - it doesn't do anything and is undefined, so is causing an error.

2. Move the contents of the <script> tag into an external JS file, then reference it using the following:
<script type="text/javascript" src="filename.js"></script>

That way, you can easily use document.write() to add the script to the page when you recreate it.

Adam

vincentms
04-17-2003, 06:23 AM
Thanks guys,

1) ... Is the SPAN tag the same as the div tag for loading values dynamically. Please look at my code again and see the SPAN tag. At first I used the DIV tag, but the linefeed was killing me.

2) I have defined the R in the 2 returns and that is not the problem.

Also, I could move the logic to an external .js but the logic still is flawed???


I dont think I was clear enough in my request, my simple little request. When I press the NEXT button it should do the getRandom function (getting a new random number) then in this function the getNext function is called which will output the new random value. Thats all.

When I press next an error occurs.

Thanks Gurus for the quick turn around.

Trying to keep my hair,
Vincent

Zürich, Switzerland

khalidali63
04-17-2003, 06:40 AM
As I mentioned before,in this ligic document.write is executed once and then the exisitng code on the page is out of scope of the page.Next time when you press "Next" button its trying to look for the getRandom method and it cant find it.Take a look at the code below



<script type="text/javascript">
var S = 0;
var p = 1;
var tpq = 5;

function getRandom() {
s = Math.round((p - 0.5) + (((tpq + 0.49999) - (p- 0.5)) * Math.random()));
getNext();
}

function getNext() {
document.getElementById("divn").innerHTML =
('<p><input type="radio" value="a" name="q10"> Senegal<br></p>') +
('<p><input type="radio" value="b" name="q10">' + " " + s + '</<br></p>') +
('<p><input type="radio" value="c" name="q10"> Nigeria<br></p>') +
('<p><input type="radio" value="d" name="q10"> Iraq<br></p>') +
('<INPUT type="button" value=" Next" onClick=" getRandom()"> ');

}

</SCRIPT>
</HEAD>

<BODY>
<FORM name="QForm">
<div id="divn">
<INPUT type="button" value="Start" onClick=" getRandom()">
</div>
</FORM>


Cheers

Khalid
EDIT:
BTW,you had only ending tag of span element,and what you are trying to do , you do not need span or div for that matter.

vincentms
04-17-2003, 07:06 AM
Hello Khalid,

YO DA MAN!!!!

Many thanks!!!!!!!!

I am back on the road to progress once again.

I now see neither the SPAN nor DIV tag was actually needed, I was misusing the SPAN tag anyway.

I will continue trying to develop my EXAM simulator. I will now try to read a text file put it into an array and depending on the RANDOM number I will use that element from the array so to get the next question (in random order).

I have made a lots of work on that part I hope now I can put it all together. I may have a problem filtering duplicates, so that the random number is never repeated. Wish me luck!

Again many many thanks.

Vincent
Zürich, Switzerland:D