Last active
January 7, 2025 17:48
-
-
Save vladyslav-dotsenko/bba0617e25587ba7a670c2b44e3ab30c to your computer and use it in GitHub Desktop.
rollSantas.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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