Never been to DZone Snippets before?

Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Permute //JavaScript Function (See related posts)


It permutes elements in an array, the "m" parameter is a boolean which determines if the function should return an array with an "index map" or the real value.

[UPDATED CODE AND HELP CAN BE FOUND HERE]


//+ Jonas Raoni Soares Silva
//@ http://jsfromhell.com/array/permute [v1.0]

permute = function( v, m ){
	for( var j, l = v.length, i = ( 1 << l ) - 1, r = new Array( i ); i; )
		for( r[--i] = [], j = l; j; i + 1 & 1 << --j && ( r[i].push( m ? j : v[j] ) ) );
	return r;
};




Example

document.write( permute( ["A", "B", "C" ], 1 ).join( "<br />" ), "<hr />" );
document.write( permute( ["A", "B", "C" ], 0 ).join( "<br />" ) );

You need to create an account or log in to post comments to this site.


Click here to browse all 5147 code snippets

Related Posts