Random Team Generator
Interactive live demo and source code tabs for Random Team Generator:
/apps/machine-coding/randomteamgenerator/index.html Open standalone ↗
function parseInput(raw) { if (!raw || !raw.trim()) return []; return raw .split(/[\n\r,]+/) .map((s) => s.trim()) .filter((s) => s.length > 0);}
function shuffle(array) { const arr = [...array]; for (let i = arr.length - 1; i > 0; i--) { const j = Math.floor(Math.random() * (i + 1)); [arr[i], arr[j]] = [arr[j], arr[i]]; } return arr;}
function distribute(participants, k) { const n = participants.length; const base = Math.floor(n / k); const rem = n % k; const teams = []; let curr = 0; for (let i = 0; i < k; i++) { const size = i < rem ? base + 1 : base; teams.push(participants.slice(curr, curr + size)); curr += size; } return teams;}
document.addEventListener('DOMContentLoaded', () => { const participantsInput = document.getElementById('participants-input'); const teamCountInput = document.getElementById('team-count-input'); const generateBtn = document.getElementById('generate-teams-button'); const resetBtn = document.getElementById('reset-button'); const errorMsg = document.getElementById('error-message'); const teamsOutput = document.getElementById('teams-output');
function showError(msg) { errorMsg.textContent = msg; errorMsg.style.display = 'block'; teamsOutput.innerHTML = ''; }
function clearError() { errorMsg.textContent = ''; errorMsg.style.display = 'none'; }
generateBtn.addEventListener('click', () => { clearError(); const names = parseInput(participantsInput.value); const countVal = teamCountInput.value.trim();
if (names.length === 0) { showError('Please enter at least one participant name.'); return; } if (!countVal) { showError('Please specify the number of teams.'); return; } const k = Number(countVal); if (isNaN(k) || !Number.isInteger(k) || k <= 0) { showError('Team count must be a positive integer.'); return; } if (k > names.length) { showError('Team count cannot exceed the number of participants.'); return; }
const shuffled = shuffle(names); const teams = distribute(shuffled, k);
teamsOutput.innerHTML = ''; teams.forEach((team, idx) => { const card = document.createElement('div'); card.className = 'team-card'; card.setAttribute('data-testid', `team-${idx + 1}`);
const header = document.createElement('h4'); header.textContent = `Team ${idx + 1} (${team.length})`; card.appendChild(header);
const ul = document.createElement('ul'); team.forEach((m) => { const li = document.createElement('li'); li.textContent = m; ul.appendChild(li); }); card.appendChild(ul); teamsOutput.appendChild(card); }); });
resetBtn.addEventListener('click', () => { participantsInput.value = ''; teamCountInput.value = ''; clearError(); teamsOutput.innerHTML = ''; });});<!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <title>Random Team Generator</title> <link rel="stylesheet" href="style.css" /></head><body> <div class="app-container"> <h2>🎲 Random Team Generator</h2> <div class="form-group"> <label for="participants-input">Participant Names:</label> <textarea id="participants-input" data-testid="participants-input" rows="5" placeholder="Enter names (comma separated or one per line)"></textarea> </div> <div class="form-group"> <label for="team-count-input">Number of Teams:</label> <input type="number" id="team-count-input" data-testid="team-count-input" placeholder="e.g. 3" /> </div> <div class="actions"> <button id="generate-teams-button" data-testid="generate-teams-button">Generate Teams</button> <button id="reset-button" data-testid="reset-button" class="secondary">Reset</button> </div> <div id="error-message" class="error-msg" style="display: none;"></div> <div id="teams-output" class="teams-container"></div> </div> <script src="index.js"></script></body></html>body { background-color: #0f172a; color: #f8fafc; font-family: system-ui, -apple-system, sans-serif; margin: 0; padding: 1rem;}
.app-container { max-width: 600px; margin: 0 auto; background: #1e293b; padding: 1.5rem; border-radius: 8px; border: 1px solid #334155;}
.form-group { margin-bottom: 1rem;}
label { display: block; margin-bottom: 0.35rem; font-weight: 600; color: #cbd5e1;}
textarea, input[type="number"] { width: 100%; padding: 0.6rem; background: #0f172a; color: #f8fafc; border: 1px solid #475569; border-radius: 6px; box-sizing: border-box;}
.actions { display: flex; gap: 0.75rem; margin-bottom: 1rem;}
button { background: #16a34a; color: white; border: none; padding: 0.5rem 1rem; border-radius: 6px; font-weight: 600; cursor: pointer;}
button.secondary { background: #475569;}
.error-msg { background: #451a1a; border: 1px solid #ef4444; color: #fca5a5; padding: 0.75rem; border-radius: 6px; margin-bottom: 1rem;}
.teams-container { display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 1rem;}
.team-card { background: #0f172a; border: 1px solid #38bdf8; border-radius: 6px; padding: 0.75rem;}
.team-card h4 { margin: 0 0 0.5rem 0; color: #38bdf8;}
.team-card ul { margin: 0; padding-left: 1.25rem;}