function print_words_permutations(words, current = []) {
if (words.length === 0) {
console.log(current.join(" "));
return;
}
words.forEach((word, i) => {
const remaining = words.slice(0, i).concat(words.slice(i + 1));
print_words_permutations(remaining, current.concat(word)); // Recursive permutation
});
}
const words = ["word-1", "word-2", "word-3"];
print_words_permutations(words);
/*
run:
word-1 word-2 word-3
word-1 word-3 word-2
word-2 word-1 word-3
word-2 word-3 word-1
word-3 word-1 word-2
word-3 word-2 word-1
*/