Skip to content

Instantly share code, notes, and snippets.

@vietnogi
Created November 13, 2019 19:04
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 vietnogi/533ea2622754029f562334d8dc4d7994 to your computer and use it in GitHub Desktop.
Save vietnogi/533ea2622754029f562334d8dc4d7994 to your computer and use it in GitHub Desktop.
const case1: string[] = ['alex', 'bob', 'john', 'alex2', 'bob2', 'john2']
const case2: string[] = ['alex', 'bob', 'john', 'alex2', 'bob2', 'john2', 'alex3']
const case3: string[] = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '10', '11', '12', '13', '14', '15', '16', '17']
const getGroupCount = (length: number) => {
if (length % 5 > 2) {
return 5
}
if (length % 4 > 2) {
return 4
}
return 3
}
const splitIntoGroups = (names: string[]): string[][] => {
const groupCount = getGroupCount(names.length)
const namesMutable = [...names]
const groups: string[][] = []
while (namesMutable.length) {
// const groupSize = namesMutable.length > 5 ? namesMutable. :
groups.push(namesMutable.slice(0, groupCount))
if (namesMutable.length < 3) {
break;
}
}
// get remainders and put them into groups with openings
groups.forEach((group) => {
if (group.length < 5) {
group.push(gour)
}
})
return groups
}
console.log(
JSON.stringify(
splitIntoGroups(case1)
) === JSON.stringify([['alex', 'bob', 'john'], ['alex2', 'bob2', 'john2']])
)
console.log(
JSON.stringify(
splitIntoGroups(case2)
) === JSON.stringify([['alex', 'bob', 'john', 'alex2'], ['bob2', 'john2', 'alex3']])
)
console.log(
JSON.stringify(
splitIntoGroups(case3)
)
)
debugger
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment