User Please construct a simple webpage which allows me to add an arbitrary amount of team members, and when I am done adding them I should be able to click a button to randomly assign them into teams.
ChatGPT
I can provide you with a simple HTML and JavaScript code to create the webpage you've described. The following code creates a webpage that allows you to add team members, input the number of teams, and click a button to randomly assign team members into teams.
1. Save the code below as an HTML file (e.g., "team_randomizer.html").
2. Open the HTML file in a web browser to use the webpage.
```html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Team Randomizer</title> <style> .result { margin-top: 20px; } </style> </head> <body> <h1>Team Randomizer</h1> <div> <label for="memberName">Member Name:</label> <input type="text" id="memberName" /> <button onclick="addMember()">Add Member</button> </div> <div> <label for="numTeams">Number of Teams:</label> <input type="number" id="numTeams" min="1" /> <button onclick="randomizeTeams()">Randomize Teams</button> </div> <div class="result" id="result"></div> <script> const members = [];
function addMember() {
const memberName = document.getElementById('memberName').value.trim();
if (memberName) {
members.push(memberName);
document.getElementById('memberName').value = '';
displayMembers();
}
}
function displayMembers() {
const result = document.getElementById('result');
result.innerHTML = `<p>Team Members: ${members.join(', ')}</p>`;
}
function randomizeTeams() {
const numTeams = parseInt(document.getElementById('numTeams').value, 10);
if (!numTeams || numTeams < 1) return;
const shuffled = [...members];
for (let i = shuffled.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
}
const teams = [];
for (let i = 0; i < numTeams; i++) {
teams[i] = [];
}
for (let i = 0; i < shuffled.length; i++) {
teams[i % numTeams].push(shuffled[i]);
}
displayResult(teams);
}
function displayResult(teams) {
const result = document.getElementById('result');
let html = '';
for (let i = 0; i < teams.length; i++) {
html += `<p>Team ${i + 1}: ${teams[i].join(', ')}</p>`;
}
result.innerHTML = html;
}
</script>
</body>
</html>
```This code will create a simple webpage where you can add team members, specify the number of teams, and randomize the teams. The team members and resulting teams will be displayed on the page.