Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Picking a variable at random (without using arrays)

Status
Not open for further replies.

starblood

Technical User
Feb 28, 2001
63
0
0
GB
If I have the following variables:

var beatle1="paul";
var beatle2="john";
var beatle3="george";
var beatle4="ringo";

Is there a way of picking one of the names at random without incorporating the use of arrays in the code?
 
try something like this:

Code:
var beatle1="paul";
var beatle2="john";
var beatle3="george";
var beatle4="ringo";

var randomNumber = Math.floor( 1 + Math.random() * 4 );
alert(randomNumber + ": " + window['beatle' + randomNumber] );



*cLFlaVA
----------------------------
[tt]somebody set up us the bomb![bomb][/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
using the window collection will work:
Code:
<script type="text/javascript">

var beatle1="paul";
var beatle2="john";
var beatle3="george";
var beatle4="ringo";

function randomBeatle() {
   var a = Math.ceil(Math.random() * 4);
   var b = window["beatle" + a];
   alert(b);
}

</script>

<input type="button" value="click me" onclick="randomBeatle()" />

-kaht

Looking for a puppy?

silky-icon-left.gif
[small]Silky Terriers are small, relatively odorless dogs that shed no fur and make great indoor pets.[/small]
silky-icon-right.gif
 
Thanks cLFlaVA and kaht. Worked a dream. Much appreciated!
 
A little late as always, but this works for me. Probobly not the best solution though.
Code:
<script type="text/javascript">

var beatle0="paul";
var beatle1="john";
var beatle2="george";
var beatle3="ringo";


function random(){
	var rannum=Math.floor(Math.random()*4);
	var ranbeatle = "beatle" + rannum; 
	eval("alert(eval(ranbeatle))"); 

}
</script>
<button onclick="random()">random</button>


 
Well it is destined for Actionscript in Flash so you never know I might end up using that "eval"!

Thanks for the alternative j4606.
 
As predicted Actionscript didn't like the window collection method, but worked with the "eval"

Tortoise and the hare j4606!

Thanks to you all for helping out. Cheers!
 
there is a perfectly good flash forum that would probably point you in a better direction than eval.

this is the javascript forum, where eval is deprecated and shunned.



*cLFlaVA
----------------------------
[tt]somebody set up us the bomb![bomb][/tt]

[URL unfurl="true"]http://www.coryarthus.com/[/url]
 
Scripting is only a small part of using Flash whilst Actionscript is virtually the same as Javascript.

Hence the reason I posted here.
 
cLFlaVA said:
oh god, he pulled an "eval"...
I provided an actionscript solution in the javascript forums so technically I pulled an eval and a me.
Also I've been searching for documentation on "window collections"???.
I can't seem to find anything too usefull, just found some posts on how to use window[] to assign variables dynamically. I'm not so much interested on the how as to the why. If anyone has some good info/reading material i'd appreciate a link.

thanks
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top