1. Home
  2. Computing & Technology
  3. JavaScript

Password Generator Tutorial

Page One

The password generator script makes quite a lot of use of the various array methods. This makes it an interesting script to look at to see how these array methods can be combined together to produce a useful program. We're not just going to look at the array code though, we'll look at all of the code in this script and what the individual statements are for so that you can see how I built this program and perhaps get some ideas of how you can build your own Javascript programs. In any case working through this tutorial will give you an insight into how I develop Javascript programs.

We'll start at the beginning of the code and work our way through the code line by line and consider what each individual statement does and what purpose it serves in the program.

var numChars = '0123456789';
var lowerChars = 'abcdefghijklmnopqrstuvwxyz';
var upperChars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ';
var otherChars = '~!@#$%^&*()-_=+[{]}\\|<>/?';

We start the script by defining all of the characters that can appear in the generated passwords and breaking them up into the four different types of characters. I initially was thinking of using string methods to generate these entries but in the end decided that just coding them was simpler to write and would produce faster running code.

function randOrd(a, b){
return (Math.round(Math.random())-0.5);
}

This function will be used to sort our array into random order.

Next page >>
Explore JavaScript
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

  1. Home
  2. Computing & Technology
  3. JavaScript

©2009 About.com, a part of The New York Times Company.

All rights reserved.