Skip to content

Instantly share code, notes, and snippets.

@timReynolds
Created November 18, 2015 23:01
Show Gist options
  • Save timReynolds/aa6baf745249321c723a to your computer and use it in GitHub Desktop.
Save timReynolds/aa6baf745249321c723a to your computer and use it in GitHub Desktop.
Code dojo pairing names
const people = ["TT", "ME", "FE", "TR", "JE", "DE", "AD"];
Array.prototype.group = group;
function group(size = 2, sizingFunc) {
const tmp = this.slice();
const output = [];
while(tmp.length) {
const groupSize = sizingFunc(tmp.length, size);
output.push(tmp.splice(0, groupSize));
}
return output;
}
function groupWithUndersize(remaining, size) {
return size;
}
function groupWithoutUndersize(remaining, size) {
return remaining % size ? size+1 : size;
}
function predefinedGroupNumber(originalGroupSize, roundFunc) {
return function(remaining, size) {
return roundFunc(originalGroupSize / size);
}
}
function randomSort() {
return 0.5 - Math.random();
};
var groups = people.sort(randomSort).group(3, predefinedGroupNumber(people.length, Math.ceil));
console.log(groups);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment