Skip to content

Commit

Permalink
Implement powerSet.js
Browse files Browse the repository at this point in the history
  • Loading branch information
fay-jai committed Jan 10, 2015
1 parent 5d7fb48 commit 3264b5c
Showing 1 changed file with 18 additions and 2 deletions.
20 changes: 18 additions & 2 deletions powerSet/powerSet.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,21 @@
* the correct set of values.
*/

var powerSet = function(str){
}
var powerSet = function (str) {
if (str.length === 0) return [''];
if (str.length === 1) return [ str ];

var result = [];
var first = str[0];
var rest = str.slice(1);
var recur = powerSet( rest );

recur.forEach( function (s) {
result.push( s );
result.push( first + s );
});

result.push( first );

return result;
};

0 comments on commit 3264b5c

Please sign in to comment.