PDA

View Full Version : Please Help With Random # Gen Script


MarvinTheRobot
02-10-2003, 04:29 PM
Hi I am MarvinTheRobot (Hitchhikers guide fan :D) I will be 16 (on feb 17), and I am in 10th grade

I am doing this as a project for my school on my free time (not H/W and no grade will be given)

It is for a writing 10 class. The Objective is to get a few weighted random genres to show up when the student clicks on it. I have compiled the basic script, but seeing as I am a JS newb, It has several problems. (due to bad codeing)

I have kept it to 4 genres for now, and the script "should" addapt to any new genres added.

The first problem is that the generator code always produces an error. It has nothing to do with the forms

The second problem is with the forms, The first button stays depressed, and nothing happens. I am not shure if java is ment to do this, and I am shure there is a better (working :))way of doing it.

If you can tell me why these things don't work, it would help alot.

Here is the basic script:


<script>

function random_genre(){
var genres=["dbq", "essay", "humor", "poem"]
var genreweight=[2, 4, 1, 3]
var totalweight=eval(genreweight.join("+"))
var weighedgenres=new Array()
var currentgenre=0
while (currentgenre<genres.langth){
for (i=0; i<genreweight[currentgenre]; i++)
weighedgenres[weighedgenres.length]=genres[currentgenre]
currentgenre++}
var randomnumber=Math.floor(Math.random()*totalweight)
document.write('<form id=form1 name=form1><input type="button" name="b2" value="Click Here To See It!" onclick=document.write(weighedgenres[randomnumber]')}
</script>

Algorithm
02-10-2003, 07:30 PM
function random_genre(){
var genres=["dbq", "essay", "humor", "poem"]
var genreweight=[2, 4, 1, 3]
var totalweight=eval(genreweight.join("+"))
var weighedgenres=new Array()
var currentgenre=0
while (currentgenre<genres.langth){
for (i=0; i<genreweight[currentgenre]; i++)
weighedgenres[weighedgenres.length]=genres[currentgenre]
currentgenre++}
var randomnumber=Math.floor(Math.random()*totalweight)
document.write('<form id=form1 name=form1><input type="button" name="b2" value="Click Here To See It!"'+
' onclick=document.write(weighedgenres[randomnumber]')}The reason your script throws an error is because of a typo (shown in red). You should also reconsider calling document.write() from an HTML object, although I'm not sure that it will actually produce an error. Use alert() instead.

MarvinTheRobot
02-11-2003, 04:10 PM
thanks alot!

I am not exactly a great speller, and it always bites me :)


Just wondering, Can I make a script that will make the window go to the page that corosponds with the genre that appears, and replace the document write script with it?

like ...[insert output here].html

MarvinTheRobot
02-11-2003, 05:09 PM
I only need to know how to get the .html added to the end, and How to get it into a differant folder in the tree.

Heres How It Looks Now:

<script>
function random_genre(){
var genres=["dbq", "essay", "humor", "poem"]
var genreweight=[2, 4, 1, 3]
var totalweight=eval(genreweight.join("+"))
var weighedgenres=new Array()
var currentgenre=0
while (currentgenre<genres.length){
for (i=0; i<genreweight[currentgenre]; i++)
weighedgenres[weighedgenres.length]=genres[currentgenre]
currentgenre++}
var randomnumber=Math.floor(Math.random()*totalweight)
var link=(weighedgenres[randomnumber])
self.location=(link)}