Created
December 6, 2023 15:15
-
-
Save branneman/44a8f05eaa8ab84b1c891a79bf50e757 to your computer and use it in GitHub Desktop.
Random person selector
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 allPeople = ['Aisha', 'Amina', 'Anya', 'Carlos', 'Chen', 'Liam', 'Nia', 'Oliver', 'Raj', 'Yuki' ] | |
const peopleAlreadySpeaking = ['Anya', 'Raj'] | |
const numberOfPeople = 2 | |
// Fisher–Yates shuffle | |
function shuffle(list) { | |
const xs = list.slice() | |
let currentIndex = xs.length | |
let randomIndex | |
// While there remain elements to shuffle | |
while (currentIndex > 0) { | |
// Pick a remaining element | |
randomIndex = Math.floor(Math.random() * currentIndex) | |
currentIndex-- | |
// And swap it with the current element | |
;[xs[currentIndex], xs[randomIndex]] = [xs[randomIndex], xs[currentIndex]] | |
} | |
return xs | |
} | |
const eligiblePeople = allPeople.filter((person) => !peopleAlreadySpeaking.includes(person)) | |
const chosenPeople = shuffle(eligiblePeople).slice(0, numberOfPeople) | |
console.log('------') | |
console.log(`Randomly chosen: ${chosenPeople.join(', ')}`) | |
console.log(`Generated on ${new Date()}`) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment