1cd9c8984f2fdeb996130d54d62a98d9

Good day guys,
Ive jst made a function to auto-generate password and the feature is look-like Cpanel password generator..
My code below looks ugly, is there other way we can refactor it?
Thanks

Arman Ortega
website: nurv7.com
profile: facebook.com/artheman

function autogeneratepassword(params) {
	this.chartype_upper 	= params.chartype_upper 
	this.chartype_lower 	= params.chartype_lower 
	this.chartype_number 	= params.chartype_number 
	this.chartype_symbol 	= params.chartype_symbol 
	this.pwlength 			= params.pwlength 
    var symbols 			= new Array("!@#$%^&*()");
    var lowercase 			= new Array("abcdefhjmnpqrstuvwxyz");
    var uppercase 			= new Array("ABCDEFGHJKLMNPQRSTUVWYXZ");
	var numbers 			= new Array("23456789");
	var pwchars 			= new Array();
    var passwd 				= document.getElementById('password');
    passwd.value 			= '';
    var passwordlength 		= this.pwlength;    
	if (this.chartype_upper) {
		pwchars = pwchars.concat(uppercase)	
	}
	if (this.chartype_lower) {
		pwchars = pwchars.concat(lowercase)	
	}
	if (this.chartype_number) {
		pwchars = pwchars.concat(numbers)	
	}	
	if (chartype_symbol) {
		pwchars = pwchars.concat(symbols)	
	}	
	pwchars = pwchars.join("")
	for ( i = 0; i < passwordlength; i++ ) {
		passwd.value += pwchars.charAt( Math.floor( Math.random() * pwchars.length ) )
	}
    return passwd.value;
	
}

Refactorings

No refactoring yet !

F9a9ba6663645458aa8630157ed5e71e

Ants

January 14, 2010, January 14, 2010 02:48, permalink

No rating. Login to rate!

I'm no JavaScript expert. Here's my first pass at refactoring to get rid of some unneeded assignments and braces.

Sige!

function autogeneratepassword(params) {
    var pwchars = "";

    if (params.chartype_upper)
        pwchars += "ABCDEFGHJKLMNPQRSTUVWYXZ";
    if (params.chartype_lower)
        pwchars += "abcdefhjmnpqrstuvwxyz";
    if (params.chartype_number)
        pwchars += "23456789";
    if (params.chartype_symbol)
        pwchars += "!@#$%^&*()";
    pwchars = pwchars.split('');

    var length  = params.pwlength; 
    var passwd  = [];

    while (length-- > 0)
        passwd.push(pwchars[Math.floor(Math.random() * pwchars.length)]);

    return passwd.join('');
}
Aacfa176a8d73ca75b90b6375151765a

paul.wilkins.myopenid.com

January 15, 2010, January 15, 2010 01:39, permalink

No rating. Login to rate!

Here's a further refinement

function autogeneratepassword(params) {
    var passwd = '',
        length = params.pwlength,
        pwchars = '';
    pwchars += (params.chartype_upper) ? 'ABCDEFGHJKLMNPQRSTUVWYXZ' : '';
    pwchars += (params.chartype_lower) ? 'abcdefhjmnpqrstuvwxyz' : '';
    pwchars += (params.chartype_number) ? '23456789' : '';
    pwchars += (params.chartype_symbol) ? '!@#$%^&*()' : '';

    while (length-- > 0) {
        passwd += pwchars.charAt[Math.floor(Math.random() * pwchars.length)];
    }
    return passwd;
}
5023923d5d5ce3d95d0f73b5dd8afc88

x-way

January 23, 2010, January 23, 2010 22:40, permalink

No rating. Login to rate!
function autogeneratepassword(params) {
    var passwd = '',
        length = params.pwlength,
        pwchars =  (params.chartype_upper) ? 'ABCDEFGHJKLMNPQRSTUVWYXZ' : '';
    pwchars += (params.chartype_lower) ? 'abcdefhjmnpqrstuvwxyz' : '';
    pwchars += (params.chartype_number) ? '23456789' : '';
    pwchars += (params.chartype_symbol) ? '!@#$%^&*()' : '';

    while (length-- > 0) {
        passwd += pwchars.charAt[Math.floor(Math.random() * pwchars.length)];
    }
    return passwd;
}
51d3ab6534b38f81ff65ec3a602d5b10

sghfg

October 13, 2010, October 13, 2010 22:29, permalink

No rating. Login to rate!

df

Your refactoring





Format Copy from initial code

or Cancel