Welcome Guest [Log In] [Register]
We hope you enjoy your visit.


You're currently viewing our forum as a guest. This means you are limited to certain areas of the board and there are some features you can't use. If you join Zeta Resources, you'll be able to access member-only sections, and use many member-only features such as requesting free skins, for both Zeta Boards and Invisionfree. It doesn't end there, we also offer free Graphics, Codes and even Affiliation! Registration is simple, fast, and completely free.


Join Zeta Resources!


If you're already a member please log in to your account to access all of our features:

Username:   Password:
Add Reply
Random 'Weighed' Something; Javascript
Topic Started: Nov 28 2007, 09:30 PM (129 Views)
Viral
Member Avatar

I love my random codes, so I mess around with them, and I found this out just a couple minutes ago.

It will write a random 'thing' from an array, more than the other stuff in the array.

So, for instance: We assign the word 'hello' the number 5, and the word 'Bonjour' the number 10, it will write 'Bonjour' twice as many times as 'hello'.

In this tutorial, you will learn how to use the following Javascript functions/expressions:
join()
eval()
x=arandomnumber

Now for the tutorial

1.

Start off your Javascript document, like no other:

Viral
 
<script>


</script>


2.

Make a new array, and assign it some names (these are what will be shown).

Viral
 
<script creator="Viral" language="Javascript">
<!--
var fruits = ["number1","number2","number3","number4","number5"]
//-->
</script>


3. Make another new array, with the same amount of variables as in the first. This time, make each variable a number. The higher it is, the more chance it will be written in the browser.

Viral
 
<script creator="Viral" language="Javascript">
<!--
var fruits = ["number1","number2","number3","number4","number5"]
var fruitweight = [1,3,2,7,5]
//-->
</script>


Let's look at the variable with the number '7' in it. It is variable number 4, but because it is in an Array (which starts at 0) it is actually in place 3. It's just the place number -1. So, in the first Array that we made, whatever is in the 3rd place (the same place as '7', gets assigned the number '7'. So, lets add up all the numbers. 1+3+2+7+5=18. This means that the variable 'number4' will be shown every 7 in 18 times.

4.


Viral
 
<script creator="Viral" language="Javascript">
<!--
var fruits = ["number1","number2","number3","number4","number5"]
var fruitweight = [1,3,2,7,5]
var totalweight = eval(fruitweight.join("+"))
//-->
</script>


This time, we added a new variable (not in an array). It is named totalweight and we used 2 new functions you may not have seen before.
eval(whatever is in here) -- will 'evaulate' what is inside the brackets as a sort of string.
fruitweight.join("+") -- will join everything in the variable 'fruitweight', seperating each oif the variables with a '+' sign.

5.

Viral
 
<script creator="Viral" language="Javascript">
<!--
var fruits = ["number1","number2","number3","number4","number5"]
var fruitweight = [1,3,2,7,5]
var totalweight = eval(fruitweight.join("+"))
var weighedfruits = new Array()
//-->
</script>


Just a new array named 'weighedfruits' to be used later.


6.

Viral
 
<script creator="Viral" language="Javascript">
<!--
var fruits = ["number1","number2","number3","number4","number5"]
var fruitweight = [1,3,2,7,5]
var totalweight = eval(fruitweight.join("+"))
var weighedfruits = new Array()
var currentfruit = 0
//-->
</script>


Now we made a new variable. We assigned it the number 0, which will be used in our loop.


7.

Viral
 
<script creator="Viral" language="Javascript">
<!--
var fruits = ["number1","number2","number3","number4","number5"]
var fruitweight = [1,3,2,7,5]
var totalweight = eval(fruitweight.join("+"))
var weighedfruits = new Array()
var currentfruit = 0
while (currentfruit<fruits.length){
}

//-->
</script>


This creates our loop. So while currentfruit (which is currently equal to 0) is less than 'fruits.length' (how many variables there are in the array 'fruits'), it will do whatever is inside the paranthesis.


8.

Viral
 
<script creator="Viral" language="Javascript">
<!--
var fruits = ["number1","number2","number3","number4","number5"]
var fruitweight = [1,3,2,7,5]
var totalweight = eval(fruitweight.join("+"))
var weighedfruits = new Array()
var currentfruit = 0
while (currentfruit<fruits.length){
for (i=0;i<fruitweight[currentfruit];i++)
}
//-->
</script>


Now we created a for loop, inside the while loop. So each time the while loop loops, the for loop loops. If you can get your head around that, your good to go. In the for loop, it assigns a varibale 'i' with the number 0. If 'i' is less than fruitweight[currentfruit], it executes whatever is in the for loop's paranthesis.
fruitweight[currentfruit] is currently equal to any of the variables in the array 'fruitweight', with the variable of currentfruit.

So this is:

fruitweight[0]
Now obviously, at the end of this loop we will create something that will increase currentfruit by 1. Then, when the for loop loops again, currentfruit will then be 1, increasing each time the loop loops.


9.

Viral
 
<script creator="Viral" language="Javascript">
<!--
var fruits = ["number1","number2","number3","number4","number5"]
var fruitweight = [1,3,2,7,5]
var totalweight = eval(fruitweight.join("+"))
var weighedfruits = new Array()
var currentfruit = 0
while (currentfruit<fruits.length){
for (i=0;i<fruitweight[currentfruit];i++)
weighedfruits[weighedfruits.length]=fruits[currentfruit]
}
//-->
</script>


This bit is very hard to explain. It is saying that:

weighedfruits[weighedfruits.length is now equal to fruits[currentfruit]

so, lets elaborate:

weighedfruits[how ever many variables in weighedfruits] = fruits[0]

fruits[0]="number1"

So,

weighedfruits[weighedfruits.length]="number1"


When the loop loops again, it will then be equal to "number2"


10.


Viral
 
<script creator="Viral" language="Javascript">
<!--
var fruits = ["number1","number2","number3","number4","number5"]
var fruitweight = [1,3,2,7,5]
var totalweight = eval(fruitweight.join("+"))
var weighedfruits = new Array()
var currentfruit = 0
while (currentfruit<fruits.length){
for (i=0;i<fruitweight[currentfruit];i++)
weighedfruits[weighedfruits.length]=fruits[currentfruit]
currentfruit++
}
//-->
</script>


Now, for the easiest, and most effective part of the script.

We added the simple function 'currentfruit++'
This will increase the currentfruit number (which is currently 0) by 1.

Then, the loop starts over, and everything is different as the current text is no longer 0, but now 1. It gets incremented by 1 each time the loop finishes.


11.

Viral
 
<script creator="Viral" language="Javascript">
<!--
var fruits = ["number1","number2","number3","number4","number5"]
var fruitweight = [1,3,2,7,5]
var totalweight = eval(fruitweight.join("+"))
var weighedfruits = new Array()
var currentfruit = 0
while (currentfruit<fruits.length){
for (i=0;i<fruitweight[currentfruit];i++)
weighedfruits[weighedfruits.length]=fruits[currentfruit]
currentfruit++
}

var randomnumber = Math.floor(Math.random()*totalweight)
//-->
</script>


So you've finished the base of the code, but now to make a random number. It makes a random number between 0, and the totalweight, which is the total of the numbers you assigned in the second array. In my case this number is 18.


12.

Viral
 
<script creator="Viral" language="Javascript">
<!--
var fruits = ["number1","number2","number3","number4","number5"]
var fruitweight = [1,3,2,7,5]
var totalweight = eval(fruitweight.join("+"))
var weighedfruits = new Array()
var currentfruit = 0
while (currentfruit<fruits.length){
for (i=0;i<fruitweight[currentfruit];i++)
weighedfruits[weighedfruits.length]=fruits[currentfruit]
currentfruit++
}

var randomnumber = Math.floor(Math.random()*totalweight)
document.write(weighedfruits[randomnumber])
//-->
</script>




You've now finished the coding! Unfortunatley, it's only done it in the browser, but doesn't actually show anything that we can see, so lets make that happen!

This writes in the browser the following:

weighedfruits[randomnumber]

weighedfruits kind of stores everything the first array does.

so it will make a random number between 0 and 18. Then write one of the objects listed. I can't explain clearly how, it just does.

This will be very confusing at first, just keep reading through it and you will finally know what everything means, certainly if you try each step out.

--Created by Viral, not to be used without permission.
Offline Profile Quote Post Goto Top
 
1 user reading this topic (1 Guest and 0 Anonymous)
« Previous Topic · Coding Tutorials · Next Topic »
Add Reply