JSFiddle - React, Tailwind, and code Playground
by YuHan Chien
JavaScript
function allRotated(list) {
function rotatedTo(i) {
var rotated = [];
rotated.push(list[i]);
return rotated.concat(list.slice(0, i))
.concat(list.slice(i + 1, list.length));
}
var all = [];
for(var i = 0; i < list.length; i++) {
all.push(rotatedTo(i));
}
return all;
}
function perm(list) {
var pls = [];
if(list.length == 0) {
pls.push([]);
} else {
allRotated(list).forEach(function(lt) {
perm(lt.slice(1, lt.length)).forEach(function(tailPl) {
var pl = [];
pl.push(lt[0]);
pls.push(pl.concat(tailPl));
});
});
}
return pls;
}
perm([1, 2, 3, 4 ,5]).forEach(function(lt) {
console.log(lt);
});