Skip to content

Instantly share code, notes, and snippets.

@suguru03
Created December 27, 2014 07:55
Show Gist options
  • Save suguru03/fad9fc37c6517bd290f8 to your computer and use it in GitHub Desktop.
Save suguru03/fad9fc37c6517bd290f8 to your computer and use it in GitHub Desktop.
function combinationLoop(list, choose, callback) {
(function loop(result, nest, index, length) {
while (index <= length + nest - choose) {
(nest === 0 ? (result = []) : result)[nest] = list[index++];
if (nest < choose - 1) {
loop(result, nest + 1, index, length);
} else {
callback(result.concat()); // 状況に応じて不要
}
}
})(null, 0, 0, list.length);
}
combinationLoop(['A', 'B', 'C', 'D', 'E'], 3, function(list) {
console.log(list);
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment