Skip to content

Instantly share code, notes, and snippets.

@adamtester
Created June 21, 2023 13:14
Show Gist options
  • Save adamtester/761eff253f55b01662f12e6e805fdc09 to your computer and use it in GitHub Desktop.
Save adamtester/761eff253f55b01662f12e6e805fdc09 to your computer and use it in GitHub Desktop.
const fs = require('fs');
const readline = require('readline');
const rl = readline.createInterface({
input: process.stdin,
output: process.stdout
});
// Load the participants and questions from the files.
fs.readFile('participants.txt', 'utf8', (err, participantsData) => {
if (err) {
console.error("Failed to read participants file", err);
return;
}
const participants = participantsData.split('\n').filter(Boolean);
fs.readFile('questions.txt', 'utf8', (err, questionsData) => {
if (err) {
console.error("Failed to read questions file", err);
return;
}
let questions = questionsData.split('\n').filter(Boolean);
// Function to generate a random question for a participant.
const askQuestion = (participant) => {
const randomIndex = Math.floor(Math.random() * questions.length);
const question = questions[randomIndex];
questions = questions.filter((q, index) => index !== randomIndex);
// Ensure the question is not asked twice.
rl.question(`${participant}, ${question}\nPress Enter for next question...\n`, (answer) => {
if (participants.length) {
askQuestion(participants.shift());
} else {
rl.close();
}
});
};
// Start asking questions.
askQuestion(participants.shift());
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment