Updated:

function permutation(a) {
    if( !(a instanceof Array) ) {
        throw new Error(a + " is not an array");
    }
    return a.reduce(function(list,element) {
        var newlist = [];
        list.forEach(function(seq) {
            for(var i=seq.length; i>=0; i--){
                var newseq = [].concat(seq);
                newseq.splice(i, 0, element);
                newlist.push(newseq);
            }
        });
        return newlist;
        }, [[]] );
}

댓글남기기