Skip to content

Instantly share code, notes, and snippets.

@shsteimer
Created December 19, 2024 17:34
Show Gist options
  • Save shsteimer/a18d2b34688c9f97ecedf6084cb01433 to your computer and use it in GitHub Desktop.
Save shsteimer/a18d2b34688c9f97ecedf6084cb01433 to your computer and use it in GitHub Desktop.
family gift exchange matcher
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