1function powerSet(arr) {
2
3 // the final power set
4 var powers = [];
5
6 // the total number of sets that the power set will contain
7 var total = Math.pow(2, arr.length);
8
9 // loop through each value from 0 to 2^n
10 for (var i = 0; i < total; i++) {
11
12 // our set that we add to the power set
13 var tempSet = [];
14
15 // convert the integer to binary
16 var num = i.toString(2);
17
18 // pad the binary number so 1 becomes 001 for example
19 while (num.length < arr.length) { num = '0' + num; }
20
21 // build the set that matches the 1's in the binary number
22 for (var b = 0; b < num.length; b++) {
23 if (num[b] === '1') { tempSet.push(arr[b]); }
24 }
25
26 // add this set to the final power set
27 powers.push(tempSet);
28
29 }
30
31 return powers;
32
33}
34
35powerSet([1, 2, 3]);
36