Skip to content

Instantly share code, notes, and snippets.

@takapiro99
Created August 6, 2021 12:03
Show Gist options
  • Save takapiro99/31605c39bc1ad2803b91312f034083d0 to your computer and use it in GitHub Desktop.
Save takapiro99/31605c39bc1ad2803b91312f034083d0 to your computer and use it in GitHub Desktop.
const chantoSort = (array) => {
for (let i = array.length - 1; i > 0; i--) {
let r = Math.floor(Math.random() * (i + 1));
let tmp = array[i];
array[i] = array[r];
array[r] = tmp;
}
return array;
};
const splitArray = (array, n) =>
array.reduce(
(a, c, i) =>
i % n == 0 ? [...a, [c]] : [...a.slice(0, -1), [...a[a.length - 1], c]],
[]
);
const participants = [...Array(10).keys()];
let count = 0;
const sets = [];
for (let i = 0; i < 5; i++) {
const shuffled = chantoSort([...participants]);
sets.push(shuffled);
count++;
}
console.log(sets.map((set, i) => `set ${i}: ${splitArray(set, 2).map((item, idx)=>(`room${idx}: ${item.join("-")}`))}`));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment