You could use a dynamic approach for any size of items, by taking one element out of the array, get the rest combinations and later put that element to the former position into the array.
Then take the actual element and add it to the result set. Then try to get the combinations of the rest array and map it to the result set as well.
function x(array) {
var i, l, element, temp, result = [];
for (i = 0, l = array.length; i < l; i++) {
element = array.splice(i, 1);
result.push(element)
temp = x(array);
if (temp.length) {
result = result.concat(temp.map(a => element.concat(a)));
}
array.splice(i, 0, element[0]);
}
return result;
}
console.log(x([...'123']));
.as-console-wrapper { max-height: 100% !important; top: 0; }
1
solved Javascript exercise: Every possible option [closed]