Power set: Difference between revisions

Content added Content deleted
Line 1,043: Line 1,043:
{{works with|SpiderMonkey}}
{{works with|SpiderMonkey}}
<lang javascript>function powerset(ary) {
<lang javascript>function powerset(ary) {
var ps = new Array(new Array());
var ps = [[]];
for (var i=0; i < ary.length; i++) {
for (var i=0; i < ary.length; i++) {
// we modify the ps array in the next loop,
for (var j = 0, len = ps.length; j < len; j++) {
// so can't use the ps.length property directly in the loop condition.
var current_length = ps.length;
for (var j = 0; j < current_length; j++) {
ps.push(ps[j].concat(ary[i]));
ps.push(ps[j].concat(ary[i]));
}
}