Click to See Complete Forum and Search --> : Selecting random variables from an Array


Grunty
08-15-2003, 04:04 AM
I have started to write a function to display a random selection from one of several username/password combinations on a webpage, but being a novice at javascript. I have ground to a halt and would be grateful if someone could point me in the right direction.

Here is what I have done so far:

function passwordlaunch(x)
{
var password = new Array();

password [0] = new Array();
password [0] [0] = "username1";
password [0] [1] = "password1";

password [1] = new Array();
password [1] [0] = "usename2";
password [1] [1] = "password2";

password [2] = new Array();
password [2] [0] = "username3";
password [2] [1] = "password3";

//values go up to username17 etc.


var passwordnumber = (math.floor(math.random() * 16);

//next bit opens a 250x100 window in the centre of the screen

window.open(x);
var cleft=(screen.availwidth-250)/2
var ctop=(screen.availheight-100)/2
var win = window.open("","win","toolbar=no, location=no, directories=no, status=no, menubar=no, resizable=no, scrollbars=no, width=250px; height=100px, left="+cleft+", top="+ctop);


// writes to the new window

win.document.open();
win.document.write(html code & font formatting removed for clarity);
win.document.write(usernameX);
win.document.write(html code & font formatting removed for clarity);
win.document.write(passwordX);
win.document.close();

}




I am not sure what to do next and how to pass the username and passwords to the new window.

I am also unsure of whether I have approached the array definition in the best way.

Any help gratefully received.

Charles
08-15-2003, 06:04 AM
<script type="text/javascript">
<!--

// First we give all Arrays a method that returns an element at random. Note that JavaScript is case sensitive
Array.prototype.random = function () {return this[Math.floor(Math.random() * this.length)]};

// And we can use Object literals to simplify the data a bit.
password = new Array();
password[0] = {user:'username0', word:'password0'};
password[1] = {user:'username1', word:'password1'};
password[2] = {user:'username2', word:'password2'};


// To use the above..
thisPassword = password.random();
document.write('<dl><dt>', thisPassword.user, '</dt><dd>', thisPassword.word, '</dd></dl>');
// -->
</script>

Grunty
08-15-2003, 07:41 AM
Many thanks,

That worked a treat.

I will have to do a bit of reading now to understand what it was that your code did

Thanks again.