document.addEventListener('DOMContentLoaded', function () {
const nameListTextarea = document.getElementById('name-list');
const pickButton = document.getElementById('pick-names');
const resultDiv = document.getElementById('result');
pickButton.addEventListener('click', function () {
const names = nameListTextarea.value.trim().split('\n');
// Check if there are enough names
if (names.length < 2) {
alert('Indtast mindst to navne for at trække navne.');
return;
}
const shuffledNames = shuffleArray(names.slice());
const assignments = assignNames(shuffledNames.slice());
// Display the assignments
let resultHtml = 'Tilfældige Navne:
';
assignments.forEach((assignment, index) => {
resultHtml += `${names[index]} skal købe en gave til ${assignment}
`;
});
resultDiv.innerHTML = resultHtml;
});
// Function to shuffle an array randomly
function shuffleArray(arr) {
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 to assign names to others randomly
function assignNames(arr) {
return arr.slice(1).concat(arr[0]);
}
});