JSFiddle - React, Tailwind, and code Playground
JavaScript
function allcombs(a) {
var all = [];
function combs(arr, str) {
var arrCopy, popped, str = str || []; // initialize str
for (var i = 0; i < arr.length; i++) {
arrCopy = arr.slice(0); // need to slice (copy), not splice (moves out and empties; affecting the for loop)
popped = arrCopy.splice(i, 1)
combs(arrCopy, str.concat(popped)); // <- need to concatenate to accumulate results
}
if (arr.length === 0) {
all.push(str);
console.log(str);
}
}
combs(a);
return all;
}
var result = JSON.stringify(allcombs(["red", "green", "blue", "purple"]), null, 4).replace(/],/g, '],<br/>');
document.body.innerHTML = result;
console.log(result);