MCQ Test Generator
MCQ Test Generator
`;
}
function generateQuizCSS() {
return `body {
font-family: Arial, sans-serif;
margin: 0;
padding: 20px;
background-color: #f4f4f4;
}
#timer {
font-size: 1.2em;
margin-bottom: 20px;
color: #333;
}
#quiz-container {
max-width: 600px;
margin: 0 auto;
padding: 20px;
background-color: #fff;
border-radius: 8px;
box-shadow: 0 2px 5px rgba(0, 0, 0, 0.1);
}
button {
background-color: #28a745;
color: white;
padding: 10px 20px;
border: none;
border-radius: 5px;
cursor: pointer;
margin-top: 20px;
}
button:hover {
background-color: #218838;
}
#results-container {
margin-top: 20px;
}
table {
width: 100%;
border-collapse: collapse;
}
th, td {
padding: 10px;
text-align: left;
border-bottom: 1px solid #ddd;
}
th {
background-color: #f2f2f2;
}
`;
}
function generateQuizJS() {
const questionsJS = questions.map(q => `
{
question: "${q.question}",
options: ${JSON.stringify(q.options)},
correct: ${q.correct}
}`).join(',');
return `const questions = [${questionsJS}
];
let timeRemaining = 300; // 5 minutes in seconds
let timerInterval;
function displayQuiz() {
const container = document.getElementById('quiz-container');
let output = '';
questions.forEach((q, index) => {
output += \`
\${q.question}
\${q.options.map((opt, i) => \`
\`).join('')}
\`;
});
container.innerHTML = output;
startTimer();
}
function startTimer() {
timerInterval = setInterval(() => {
const timerDisplay = document.getElementById('timer');
if (timeRemaining <= 0) {
clearInterval(timerInterval);
alert('Time is up! Submitting the quiz.');
submitQuiz();
} else {
const minutes = Math.floor(timeRemaining / 60);
const seconds = timeRemaining % 60;
timerDisplay.innerText = \`Time Remaining: \${minutes}:\${seconds < 10 ? '0' : ''}\${seconds}\`;
timeRemaining--;
}
}, 1000);
}
function submitQuiz() {
clearInterval(timerInterval);
const results = [];
questions.forEach((q, index) => {
const selectedOption = document.querySelector(\`input[name="q\${index}"]:checked\`);
const selectedValue = selectedOption ? parseInt(selectedOption.value) : -1;
const result = {
question: q.question,
selected: selectedValue === -1 ? 'No answer' : q.options[selectedValue],
correct: selectedValue === q.correct
};
results.push(result);
});
displayResults(results);
}
function displayResults(results) {
const resultsContainer = document.getElementById('results-container');
let output = '
Results
Question | Your Answer | Correct? |
';
results.forEach(result => {
output += \`
\${result.question} |
\${result.selected} |
\${result.correct ? 'Correct' : 'Incorrect'} |
\`;
});
output += '
';
resultsContainer.innerHTML = output;
}
// Initializing the quiz
document.addEventListener('DOMContentLoaded', () => {
displayQuiz();
document.getElementById('submit').addEventListener('click', submitQuiz);
});
`;
}
function downloadFile(filename, content) {
const blob = new Blob([content], { type: 'text/plain' });
const link = document.createElement('a');
link.href = URL.createObjectURL(blob);
link.download = filename;
link.click();
}