Skip to content

Instantly share code, notes, and snippets.

@gdsouza1992
Created May 27, 2021 23:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save gdsouza1992/235cf0bce3d1a83eaffe647a49a271b2 to your computer and use it in GitHub Desktop.
Save gdsouza1992/235cf0bce3d1a83eaffe647a49a271b2 to your computer and use it in GitHub Desktop.
Secret Santa
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