First let's create the gen.
We are going to make an array.
var qua=new Array() qua[0]="very poor" //Notice that the number above starts with 0 not 1. //It's a common mistake to start with one which will result in //"undifined" showing up sometimes qua[1]="poor" qua[2]="average" qua[3]="slightly above average" qua[4]="good" qua[5]="Great" qua[6]="Masterwork"
Now we have the quality set. Now we need to make the sword
var sword=new Array() sword[0]="short sword" sword[1]="long sword" sword[2]="broad sword"Remember to start with 0!
Both those go in the head tag of your document. Feel free to add more to this generator such as length,weight,element(if any),etc...
To make this work, in the body of your document add this
<script type="text/javascript"> document.write(qua[Math.floor(Math.random()*6)]) document.write( ) // ↑ to add a space between the quality and the sword name document.write(sword[Math.floor(Math.random()*2)])This will write the results when the page loads.
Why Math.floor, why not Math.round?
Math.floor always rounds down, and Math.round will round either way. Without the .floor we would get a decimal like 4.5684527564 instead we want an integer, so we use Math.floor.
Make sure you capitalize the M in math.
It may be possible to generate images. If you know the answer to this, please post in the message boards.
If you want to make it look cool you could create an image as a background for a page. The image could be anything you want, it could be an ipod,a cow,a doughnut,even a swimming pool! Then just on part of the image add the code to make your results appear.
This could also be used for a random sign generator.
If you want to see a few generators you can visit my old site:
Toxigen-scripting toxic generators!
I won't update it anymore, but it will work as examples.
I hope you enjoyed the tutorial, please comment!


Sign In
Create Account


Back to top









