Skip to content

Instantly share code, notes, and snippets.

@vladyslav-dotsenko
Last active January 7, 2025 17:48
Show Gist options
  • Save vladyslav-dotsenko/bba0617e25587ba7a670c2b44e3ab30c to your computer and use it in GitHub Desktop.
Save vladyslav-dotsenko/bba0617e25587ba7a670c2b44e3ab30c to your computer and use it in GitHub Desktop.
rollSantas.js
const shuffle = input => {
const array = [...input]
for (let i = array.length - 1; i >= 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array
}
const slide1 = ([first, ...rest]) => ([...rest, first])
const zip = (listA, listB) => listA.map((a, i) => [a, listB[i]])
const rollSantas = names => {
const shuffledNames = shuffle(names)
return zip(shuffledNames, slide1(shuffledNames))
}
const testInput = ['a', 'b', 'c', 'd', 'e', 'f', 'g']
console.log(rollSantas(testInput))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment