I found myself in need of a pronounceable but randomly generated string tonight, and decided to indulge myself with writing a single line of inconsequential Javascript that would output such a string when pasted into the address line of a webbrowser.

Which led to the following line of code, that only a blind mother could truly love.

Copy the the line to the address field of you browser or press this link to see the magic happening.

javascript:rc=function(c){return Math.floor(Math.random()*c)};do{for(s='',i=0;i<7;i++){s+='aeioubcdfghjklmnpqrstvwxyz'.split('')[i%2?5+rc(19):rc(5)]};} while(!confirm('Suggestion: '+s+'\n'+'Decline for new suggestion'))

It’s been a few years since I last did any significant amount of Javascript programing, but I feel confident that the result of executing this script will be a dialogue that asks the user to suggest a string of 7 characters which alternate between vowels and consonants chosen at random.

The most fun part of writing this one-liner was making the string as short as possible. And I even stumbled upon a good use for the very under utilized do-while loop in the process.

I’m satisfied with the result, but I’d love to see examples that achieving the same functionality with an even shorter piece of code.

For good measure, I’d also like to provide a rewrite of the above which adheres more closely to my criteria for good source code:

function RandWordPass(){ /* class constructor */
/* initialization
Set up methods and variables. Use closure techniques to enforce
object encapsulation.*/

/****************************************/
/* begin internal definitions (closures)*/
/****************************************/
var default_length = 7;

/* helper method
returns random number from 0 to ceil-1 */
var ceiled_rand_int = function(ceil){
return Math.floor(Math.random()*ceil)
}

/* constant arrays of characters*/
var vowels = 'aeiou'.split('');
var consonants = 'bcdfghjklmnpqrstvwxyz'.split('');

/****************************************/
/* end internal definitions (closures)*/
/****************************************/

/****************************************/
/* begin method definitions */
/****************************************/
/* function: generate(length)
Generate a random string alternating
between vowels and consonants
Arguments:
length: password length, optional
Returns: String of requested length */
this.generate = function(length){
length= length||default_length; //falback to default value

/*bind closures to method*/
var rc = ceiled_rand_int;
var c = consonants;
var v = vowels;

var s = '';
for(var i=0;i<length;i++){
if(i%2)
s+=c[rc(c.length)]
else
s+=v[rc(v.length)]
}
return s;
}
/* function: suggestions_box(length)
Run a dialogue box that suggest passwords
as long as the user keeps declining the dialogue.
Arguments:
length: password length, optional
Returns: Does not return */
this.suggestions_box = function(length){
do{
var s='Suggestion: ';
s+=this.generate(length);
s+="\nDecline for new suggestion"
}
while(!confirm(s));
}

/****************************************/
/* end method definitions */
/****************************************/
}

Feel free to provide suggestions for improvements and/or point out obvious errors in the above, as an exchange on the art of writing Javascript which doesn’t leave dogs, children and pregnant women crying due to being such an incomprehensibly broken mess is a suitable pastime for Gentlemen Of Code to engage in.

Pilen
Offline
Sv: The Joy Of Javascript One Liners

Got me thinking about generating names in my current side-project, a small game called MosAML.
Sadly i wont have time to play around with it, for the next 24 hours or so :/