-
Notifications
You must be signed in to change notification settings - Fork 2
/
callon.js
86 lines (75 loc) · 2.17 KB
/
callon.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
var fs = require('fs');
var data = require('./students');
//CallOn Object constructor
function CallOn(obj, opts) {
this.data = obj
this.students = Object.keys(this.data);
this.options = opts;
}
CallOn.prototype.run = function() {
// TODO: MONITOR STATISTICS RELATED ON WHO'S BEEN CALLED
// OR GROUPED TOGETHER.
if (!!this.options) {
return this.runOpt(this.options[0]);
} else {
return this.runOpt();
}
};
CallOn.prototype.runOpt = function(opt) {
//TODO: ADD STATS OPTION THAT OUTPUTS STATS RELATED TO
// THE NUMBER OF TIMES SOMEONE WAS CALLED
switch (opt) {
case '-groups':
return this.groupsOf(3);
case '-g':
return this.runOpt('-groups');
case '-pairs':
return this.groupsOf(2);
case '-p':
return this.runOpt('-pairs');
default:
return this.callon();
}
};
//takes a variable that specifies group size and outputs random groups of that size
CallOn.prototype.groupsOf = function(groupSize) {
var numberOfGroups = Math.ceil(this.students.length / groupSize);
var groups = {};
//helper function that assigns a person to a group randomly
var assign = function(person) {
var groupNum = Math.floor(numberOfGroups * Math.random());
var group = groups[groupNum] || [];
switch (true) {
case group.length === 0:
return groupNum;
case (group.length === groupSize):
return assign(person);
case (person in group):
return assign(person);
default:
return groupNum;
}
};
this.students.forEach(function(student) {
var assignedNum = assign(student);
var assignedGroup = groups[assignedNum] || [];
assignedGroup.push(student);
groups[assignedNum] = assignedGroup;
});
return groups;
};
//returns person as a string
CallOn.prototype.callon = function() {
var index = Math.floor(Math.random() * this.students.length);
var person = this.students[index];
return person;
};
// Class method that saves the file
CallOn.write = function() {
var file = JSON.stringify(this.data);
fs.writeFile('students.json', file, function(err) {
if (err) console.log(err);
console.log('DONE');
});
};
module.exports = CallOn;