-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
81 lines (71 loc) · 2.45 KB
/
index.js
File metadata and controls
81 lines (71 loc) · 2.45 KB
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
/**
* Main application logic for the team generator.
* @namespace TeamGenerator
*/
"use strict";
document.addEventListener("alpine:init", () => {
Alpine.data("teamGenerator", () => ({
/**
* The comma-separated string of member names.
* @type {string}
*/
membersInput: "",
/**
* The desired number of teams.
* @type {number}
*/
numberOfTeams: 2,
/**
* The array of generated teams, where each team is an array of members.
* @type {Array<Array<string>>}
*/
generatedTeams: [],
/**
* Shuffles an array in place using the Fisher-Yates (Knuth) algorithm.
* @param {Array<any>} array - The array to shuffle.
* @returns {Array<any>} The shuffled array.
*/
shuffleArray(array) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
return array;
},
/**
* Parses a string of comma-separated names into a cleaned array of strings.
* @param {string} namesString - The string of names.
* @returns {Array<string>} The cleaned array of member names.
*/
parseMembers(namesString) {
return namesString
.split(",")
.map((name) => name.trim())
.filter((name) => name !== "");
},
/**
* Generates and distributes members into the specified number of teams.
*/
generateTeams() {
// Reset the teams display
this.generatedTeams = [];
// Parse and shuffle the member names
const members = this.parseMembers(this.membersInput);
if (members.length === 0) {
return; // Do nothing if there are no members
}
const shuffledMembers = this.shuffleArray([...members]);
// Create and populate teams
const newTeams = Array.from(
{ length: this.numberOfTeams },
() => []
);
shuffledMembers.forEach((member, index) => {
const teamIndex = index % this.numberOfTeams;
newTeams[teamIndex].push(member);
});
// Update the state
this.generatedTeams = newTeams;
},
}));
});