Created
November 1, 2018 09:50
-
-
Save thomasleduc/7d98c4760e0dadc420539dea5561cb1b to your computer and use it in GitHub Desktop.
Randomly assign gifts for Xmas
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 fs = require('fs'); | |
Array.prototype.flat = function () { | |
return this.reduce((acc, val) => acc.concat(val), []); | |
}; | |
const participants = [ | |
'Nicolas', | |
'Mariette', | |
'Thomas', | |
'Charline', | |
'Jojo', | |
'Marine', | |
'Axelle', | |
'Justine', | |
'Natacha', | |
'Lili', | |
'Sylvain', | |
'Julie', | |
'Christophe', | |
'Fech', | |
'Hadrien', | |
'Noemie', | |
'Roland', | |
'M. Pascale', | |
'Mathieu', | |
'Clement', | |
'Florent', | |
'Aurélie', | |
'Clara', | |
'Margaux', | |
'Romain', | |
'Kathy', | |
'Simon', | |
]; | |
const notCompatibles = [ | |
['Nicolas', 'Mariette', 'Thomas', 'Charline'], | |
['Jojo', 'Marine', 'Axelle', 'Justine'], | |
['Sylvain', 'Julie', 'Christophe'], | |
['Fech', 'Hadrien', 'Noemie'], | |
['M. Pascale', 'Mathieu', 'Clement'], | |
['Florent', 'Aurélie', 'Clara', 'Margaux'], | |
['Romain', 'Kathy', 'Simon'], | |
]; | |
const toExclude = giver => | |
notCompatibles | |
.filter(notCompatible => notCompatible.includes(giver)) | |
.flat(); | |
const randomIndex = (length, minIndex = 0) => | |
Math.floor(Math.random() * length) + minIndex; | |
const pickRandomly = (arr) => arr[randomIndex(arr.length)]; | |
const pickRandomlyWithException = (arr, exceptedValues) => { | |
const arrWithoutException = arr.filter(el => !exceptedValues.includes(el)); | |
return pickRandomly(arrWithoutException); | |
}; | |
let receivers = [...participants]; | |
const gifts = participants.map((giver, index) => { | |
// pick in left receivers except the current giver | |
const receiver = pickRandomlyWithException(receivers, [...toExclude(giver), giver]); | |
// remove receiver from the left receivers | |
receivers = receivers.filter(el => el !== receiver); | |
// return the couple giver/receiver | |
return { giver, receiver }; | |
}); | |
fs.writeFile('/Users/thomas/Desktop/cadeaux_noel/result.txt', JSON.stringify(gifts, null, 2), () => { | |
console.log('The drawing lots is done'); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment