-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathteamMatching.js
68 lines (58 loc) · 1.5 KB
/
teamMatching.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
const members = require('./members');
// 배열을 무작위로 섞어주는 함수
const shuffle = (arr) => {
for (let i = 0; i < arr.length; i++) {
const randomIndex = Math.floor(Math.random() * arr.length);
const temp = arr[i];
arr[i] = arr[randomIndex];
arr[randomIndex] = temp;
}
};
// OB와 YB로 나누어주는 함수
const filterOBYB = (arr, OBs, YBs) => {
arr.forEach((member) => {
if (member.group === 'OB') {
OBs.push(member);
} else {
YBs.push(member);
}
});
};
// 팀 개수에 따라 팀매칭
const matchingByTeamNumber = (numberOfTeam, OBs, YBs) => {
const teams = [];
for (let i = 0; i < numberOfTeam; i++) {
teams.push([]);
}
let index = 0;
for (let i = 0; i < OBs.length; i++) {
teams[index].push(OBs[i]);
index++;
if (index === numberOfTeam) {
index = 0;
}
}
index = numberOfTeam - 1;
for (let i = 0; i < YBs.length; i++) {
teams[index].push(YBs[i]);
index--;
if (index === -1) {
index = numberOfTeam - 1;
}
}
return teams;
};
// 멤버수에 따라 팀매칭
const matchingByMemberNumber = (numberOfMember, OBs, YBs) => {
const allMembers = OBs.length + YBs.length;
const numberOfTeam = Math.floor(allMembers / numberOfMember);
return matchingByTeamNumber(numberOfTeam, OBs, YBs);
};
// main
const OBs = [];
const YBs = [];
filterOBYB(members, OBs, YBs);
shuffle(OBs);
shuffle(YBs);
console.log(matchingByTeamNumber(5, OBs, YBs));
console.log(matchingByMemberNumber(4, OBs, YBs));