Over a million developers have joined DZone.

Simple Permutation //JavaScript Function

·


[UPDATED CODE AND HELP CAN BE FOUND HERE]


example


var a = ["A", "B", "C", "D"], j = permute(a);
document.write(
    "

", a.join(" - "), " = ", j.length, "

", j.join("
") );
code //+ Jonas Raoni Soares Silva //@ http://jsfromhell.com/array/permute [v1.0] permute = function(v, m){ //v1.0 for(var p = -1, j, k, f, r, l = v.length, q = 1, i = l + 1; --i; q *= i); for(x = [new Array(l), new Array(l), new Array(l), new Array(l)], j = q, k = l + 1, i = -1; ++i < l; x[2][i] = i, x[1][i] = x[0][i] = j /= --k); for(r = new Array(q); ++p < q;) for(r[p] = new Array(l), i = -1; ++i < l; !--x[1][i] && (x[1][i] = x[0][i], x[2][i] = (x[2][i] + 1) % l), r[p][i] = m ? x[3][i] : v[x[3][i]]) for(x[3][i] = x[2][i], f = 0; !f; f = !f) for(j = i; j; x[3][--j] == x[2][i] && (x[3][i] = x[2][i] = (x[2][i] + 1) % l, f = 1)); return r; };
Topics:

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}