Skip to content

Instantly share code, notes, and snippets.

@aichholzer
Last active December 21, 2015 01:05
Show Gist options
  • Save aichholzer/4e87c221b7facea7efb4 to your computer and use it in GitHub Desktop.
Save aichholzer/4e87c221b7facea7efb4 to your computer and use it in GitHub Desktop.
Shuffle & match a list of names for whatever occasion.
'use strict';
class Shufflr {
constructor () {
process.argv.splice(0, 2);
this.people = process.argv.sort();
this.selectedPeople = [];
this.results = {};
}
names (index) {
index = index || 0;
let availablePeople = this.people.filter(x => this.selectedPeople.indexOf(x) == -1);
let receiver = availablePeople[Math.round(Math.random() * (availablePeople.length - 1))];
if (this.people[index] === receiver) {
this.selectedPeople = [];
return this.names(0);
}
this.selectedPeople.push(receiver);
this.results[this.people[index]] = receiver;
index += 1;
if (index < this.people.length) {
return this.names(index);
}
return this.results;
}
}
let shuffle = new Shufflr();
console.log(shuffle.names());
@aichholzer
Copy link
Author

Use: node shufflr.js [name ]
Example: node shufflr.js peter paul marry jane andrew

Will output something like:

{
  andrew: 'paul',
  jane: 'peter',
  marry: 'andrew',
  paul: 'marry',
  peter: 'jane'
}

Enjoy!
:)

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