Created
December 19, 2024 17:34
-
-
Save shsteimer/a18d2b34688c9f97ecedf6084cb01433 to your computer and use it in GitHub Desktop.
family gift exchange matcher
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 run = () => { | |
const families = [ | |
['aaron'], | |
['sean'], | |
['drew', 'michelle'], | |
['jessica', 'eric'], | |
]; | |
const flattened = []; | |
families.forEach((fam) => { | |
fam.forEach((name) => { | |
flattened.push(name); | |
}); | |
}); | |
const matches = {}; | |
const isValid = (gifter, giftee) => { | |
if (gifter === giftee) return false; | |
if (Object.keys(matches).includes(gifter)) return false; | |
if (Object.values(matches).includes(giftee)) return false; | |
const gifterFamily = families.find((fam) => { | |
return fam.includes(gifter); | |
}); | |
if (gifterFamily.includes(giftee)) return false; | |
return true; | |
}; | |
while (Object.keys(matches).length < flattened.length) { | |
const gifter = flattened[Math.floor(Math.random() * flattened.length)]; | |
const giftee = flattened[Math.floor(Math.random() * flattened.length)]; | |
if (isValid(gifter, giftee)) { | |
matches[gifter] = giftee; | |
} | |
} | |
console.log(matches); | |
}; | |
run(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment