Created
May 27, 2021 23:30
-
-
Save gdsouza1992/235cf0bce3d1a83eaffe647a49a271b2 to your computer and use it in GitHub Desktop.
Secret Santa
This file contains 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 kids = [ | |
'ifeest@hotmail.com', | |
'osawayn@gmail.com', | |
'aidan.stracke@hotmail.com', | |
'mortimer.miller@ohara.com', | |
'runte.luella@hotmail.com', | |
'jcassin@crona.net', | |
'windler.ardith@upton.com', | |
'dean.kozey@tillman.info', | |
'gtorp@dibbert.com', | |
'alysa33@yahoo.com', | |
] | |
const shuffle = (arr: string[]) => { | |
let count = 0; | |
while(count < arr.length) { | |
const elementA = arr[count]; | |
const randomPosition = Math.floor(Math.random() * count); | |
const elementB = arr[randomPosition]; | |
// Swap a and b | |
arr[count] = elementB; | |
arr[randomPosition] = elementA; | |
count++; | |
} | |
return arr; | |
} | |
const makeSecretSantaPairs = (kids: string[]) => { | |
const shuffledKids = shuffle(kids); | |
const pairs: string[][] = []; | |
const length = shuffledKids.length; | |
for(let i = 0; i < shuffledKids.length; i++) { | |
// console.log(i, ((i+1) % length)); | |
pairs.push([shuffledKids[i], shuffledKids[((i+1) % length)]]); | |
} | |
return pairs; | |
} | |
console.log(makeSecretSantaPairs(kids)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment