Created
October 29, 2015 08:18
-
-
Save simbo/134b493d7705fd5f9879 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
'use strict'; | |
var values = ['A', 'B', 'C', 'D']; | |
function getCombinations(parts) { | |
var mix = function(rest) { | |
return rest.length === 0 ? '' : rest.join(''); | |
}; | |
var combine = function(combinations, firstPart, i, _parts) { | |
var rest = _parts.filter(function(part) { | |
return firstPart !== part; | |
}); | |
if (rest.length > 0) rest = rest.reduce(combine, []); | |
console.log(rest); | |
_parts.forEach(function(combination) { | |
combinations.push(firstPart + combination.join('')); | |
}); | |
return combinations; | |
}; | |
return parts.reduce(combine, []); | |
} | |
console.log(getCombinations(values)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment