Skip to content

Instantly share code, notes, and snippets.

@dijonkitchen
Last active November 5, 2018 15:49
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 dijonkitchen/eef2194623662cae8ea5a4d94d6162a4 to your computer and use it in GitHub Desktop.
Save dijonkitchen/eef2194623662cae8ea5a4d94d6162a4 to your computer and use it in GitHub Desktop.
// Inputs: List of names
// Outputs: Groups of size 3-5, randomized
// Pseudocode:
// Take array of names
// Shuffle this array
// Split into groups
// Check if length is less than 3
// Return randomized list
// Split shuffled array into groups of 3-5
// Return an array of arrays (each with group of people)
const shuffle = (array) => {
const shuffledArray = array.slice()
for (let i = (shuffledArray.length - 1); i >= 0; i--) {
const randomIndex = Math.floor(Math.random() * i)
const temp = shuffledArray[randomIndex]
shuffledArray[randomIndex] = shuffledArray[i]
shuffledArray[i] = temp
}
return shuffledArray
}
const randomGroups = (names, minSize = 3) => {
const shuffledNames = shuffle(names)
if (names.length <= minSize) {
return shuffledNames
} else {
const allGroups = []
let tempGroup = []
for (let i = 0; i < names.length; i++) {
if (tempGroup.length < minSize) {
tempGroup.push(shuffledNames[i])
} else {
allGroups.push(tempGroup)
tempGroup = [shuffledNames[i]]
}
}
let remainder = names.length % minSize
for (let i = 0; i < remainder; i++) {
allGroups[allGroups.length - 1].push(shuffledNames.pop())
}
return allGroups
}
}
const letters = 'abcdefghijklmnopqrstuvwxyz'.split('')
console.log(randomGroups(letters))
@dijonkitchen
Copy link
Author

dijonkitchen commented May 14, 2018

See revisions for previous version before refactoring!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment