Creating a comprehensive Q&A application involves several components: designing a structured input format, developing a user-friendly and aesthetically pleasing UI, and implementing the core functionality using JavaScript. Below is a complete solution that fulfills your requirements.
Overview
- Input Format: We'll use JSON for inputting Q&A sets because it is both human-readable and easy to parse programmatically.
- UI Design: We'll utilize HTML5, CSS3, and JavaScript to build the UI. For a beautiful and responsive design, we'll incorporate Bootstrap 5 and Font Awesome for icons.
- Functionality: The application will allow users to add, delete, select Q&A sets, conduct Q&A sessions with various options, track success rates, and display statistics.
Input Format Design
We'll structure the Q&A data in JSON format. Each Q&A set will have a unique name and an array of questions. Each question will have the following properties:
id: A unique identifier for the question.
question: The question text.
answer: The answer text.
successRate: The success rate (initialized to 0).
attempts: Number of attempts (initialized to 0).
correct: Number of correct answers (initialized to 0).
Example JSON Structure
[
{
"setName": "General Knowledge",
"questions": [
{
"id": 1,
"question": "What is the capital of France?",
"answer": "Paris",
"successRate": 0,
"attempts": 0,
"correct": 0
},
{
"id": 2,
"question": "Who wrote 'To Kill a Mockingbird'?",
"answer": "Harper Lee",
"successRate": 0,
"attempts": 0,
"correct": 0
}
]
},
{
"setName": "Science",
"questions": [
{
"id": 1,
"question": "What is the chemical symbol for water?",
"answer": "H2O",
"successRate": 0,
"attempts": 0,
"correct": 0
}
]
}
]
Complete Application Code
Below is the complete HTML, CSS, and JavaScript code for the Q&A application. You can save this as an index.html file and open it in your browser.
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Q&A Application</title>
<meta name="viewport" content="width=device-width, initial-scale=1">
<!-- Bootstrap 5 CSS -->
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/css/bootstrap.min.css" rel="stylesheet">
<!-- Font Awesome for Icons -->
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.4.0/css/all.min.css">
<style>
body {
background-color: #f8f9fa;
}
.container {
margin-top: 30px;
}
.card {
margin-bottom: 20px;
}
.question-card {
min-height: 200px;
}
.success-rate {
font-size: 0.9rem;
color: #6c757d;
}
</style>
</head>
<body>
<div class="container">
<h1 class="text-center mb-4">Q&A Application</h1>
<div class="row">
<!-- Q&A Sets List -->
<div class="col-md-4">
<div class="card">
<div class="card-header bg-primary text-white">
Q&A Sets
</div>
<div class="card-body">
<ul class="list-group" id="qa-sets-list">
<!-- Dynamically populated -->
</ul>
</div>
<div class="card-footer text-end">
<button class="btn btn-success" data-bs-toggle="modal" data-bs-target="#addSetModal"><i class="fas fa-plus"></i> Add Set</button>
</div>
</div>
</div>
<!-- Q&A Set Management -->
<div class="col-md-8">
<div id="main-section">
<p class="text-center text-muted">Select a Q&A set to manage or start a session.</p>
</div>
</div>
</div>
</div>
<!-- Add Q&A Set Modal -->
<div class="modal fade" id="addSetModal" tabindex="-1" aria-labelledby="addSetModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<form id="add-set-form">
<div class="modal-header">
<h5 class="modal-title" id="addSetModalLabel">Add New Q&A Set</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<div class="mb-3">
<label for="set-name" class="form-label">Set Name</label>
<input type="text" class="form-control" id="set-name" required>
</div>
<div class="mb-3">
<label for="qa-input" class="form-label">Q&A Data (JSON)</label>
<textarea class="form-control" id="qa-input" rows="10" placeholder='[
{
"id": 1,
"question": "Question?",
"answer": "Answer",
"successRate": 0,
"attempts": 0,
"correct": 0
}
]'></textarea>
<div class="form-text">Enter Q&A in JSON format.</div>
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary">Add Set</button>
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
</div>
</form>
</div>
</div>
</div>
<!-- Manage Q&A Set Modal -->
<div class="modal fade" id="manageSetModal" tabindex="-1" aria-labelledby="manageSetModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Manage Q&A Set: <span id="manage-set-name"></span></h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<button class="btn btn-success mb-3" id="add-question-btn"><i class="fas fa-plus"></i> Add Question</button>
<table class="table table-striped">
<thead>
<tr>
<th>#</th>
<th>Question</th>
<th>Answer</th>
<th>Success Rate</th>
<th>Actions</th>
</tr>
</thead>
<tbody id="questions-table-body">
<!-- Dynamically populated -->
</tbody>
</table>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Close</button>
</div>
</div>
</div>
</div>
<!-- Add/Edit Question Modal -->
<div class="modal fade" id="addEditQuestionModal" tabindex="-1" aria-labelledby="addEditQuestionModalLabel" aria-hidden="true">
<div class="modal-dialog">
<div class="modal-content">
<form id="add-edit-question-form">
<div class="modal-header">
<h5 class="modal-title" id="addEditQuestionModalLabel">Add/Edit Question</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<input type="hidden" id="question-id">
<div class="mb-3">
<label for="question-text" class="form-label">Question</label>
<textarea class="form-control" id="question-text" rows="3" required></textarea>
</div>
<div class="mb-3">
<label for="answer-text" class="form-label">Answer</label>
<textarea class="form-control" id="answer-text" rows="3" required></textarea>
</div>
</div>
<div class="modal-footer">
<button type="submit" class="btn btn-primary">Save Question</button>
<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">Cancel</button>
</div>
</form>
</div>
</div>
</div>
<!-- Q&A Session Modal -->
<div class="modal fade" id="sessionModal" tabindex="-1" aria-labelledby="sessionModalLabel" aria-hidden="true">
<div class="modal-dialog modal-lg">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title">Q&A Session: <span id="session-set-name"></span></h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close" id="close-session-btn"></button>
</div>
<div class="modal-body">
<!-- Session Options -->
<div id="session-options">
<div class="mb-3">
<label class="form-label">Select Questions to Answer:</label>
<select class="form-select" id="questions-select" multiple>
<!-- Dynamically populated -->
</select>
<div class="form-text">Hold down the Ctrl (windows) / Command (Mac) button to select multiple options.</div>
</div>
<div class="mb-3">
<label class="form-label">Question Order:</label>
<select class="form-select" id="question-order">
<option value="insertion">Insertion Order</option>
<option value="successRate">Success Rate (Lowest First)</option>
</select>
</div>
<div class="mb-3">
<label class="form-label">Shuffle Questions:</label>
<select class="form-select" id="shuffle-questions">
<option value="no">No</option>
<option value="yes">Yes</option>
</select>
</div>
<button class="btn btn-primary" id="start-session-btn">Start Session</button>
</div>
<!-- Session Content -->
<div id="session-content" class="d-none">
<div class="card question-card">
<div class="card-body">
<h5 class="card-title">Question <span id="current-question-number"></span> of <span id="total-questions"></span></h5>
<p class="card-text" id="current-question-text"></p>
<button class="btn btn-info" id="show-answer-btn"><i class="fas fa-eye"></i> Show Answer</button>
<p class="mt-3 d-none" id="current-answer-text"></p>
</div>
</div>
<div class="d-flex justify-content-between">
<button class="btn btn-success" id="mark-correct-btn"><i class="fas fa-check"></i> Correct</button>
<button class="btn btn-danger" id="mark-wrong-btn"><i class="fas fa-times"></i> Wrong</button>
</div>
</div>
<!-- Session Stats -->
<div id="session-stats" class="d-none">
<h5>Session Statistics</h5>
<p>Total Questions: <span id="stats-total"></span></p>
<p>Correct Answers: <span id="stats-correct"></span></p>
<p>Wrong Answers: <span id="stats-wrong"></span></p>
<p>Success Rate: <span id="stats-success-rate"></span>%</p>
<button class="btn btn-secondary" id="close-stats-btn">Close</button>
</div>
</div>
</div>
</div>
</div>
<!-- Bootstrap 5 JS and dependencies -->
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0/dist/js/bootstrap.bundle.min.js"></script>
<!-- Application JavaScript -->
<script>
// Initial Q&A data
let qaSets = [];
// Currently selected set
let selectedSetIndex = null;
// Session variables
let sessionQuestions = [];
let currentQuestionIndex = 0;
let sessionCorrect = 0;
let sessionWrong = 0;
// DOM Elements
const qaSetsList = document.getElementById('qa-sets-list');
const addSetForm = document.getElementById('add-set-form');
const manageSetModal = new bootstrap.Modal(document.getElementById('manageSetModal'));
const manageSetName = document.getElementById('manage-set-name');
const questionsTableBody = document.getElementById('questions-table-body');
const addQuestionBtn = document.getElementById('add-question-btn');
const addEditQuestionForm = document.getElementById('add-edit-question-form');
const sessionModal = new bootstrap.Modal(document.getElementById('sessionModal'));
const sessionSetName = document.getElementById('session-set-name');
const questionsSelect = document.getElementById('questions-select');
const questionOrder = document.getElementById('question-order');
const shuffleQuestions = document.getElementById('shuffle-questions');
const startSessionBtn = document.getElementById('start-session-btn');
const sessionOptions = document.getElementById('session-options');
const sessionContent = document.getElementById('session-content');
const currentQuestionNumber = document.getElementById('current-question-number');
const totalQuestions = document.getElementById('total-questions');
const currentQuestionText = document.getElementById('current-question-text');
const showAnswerBtn = document.getElementById('show-answer-btn');
const currentAnswerText = document.getElementById('current-answer-text');
const markCorrectBtn = document.getElementById('mark-correct-btn');
const markWrongBtn = document.getElementById('mark-wrong-btn');
const sessionStats = document.getElementById('session-stats');
const statsTotal = document.getElementById('stats-total');
const statsCorrect = document.getElementById('stats-correct');
const statsWrong = document.getElementById('stats-wrong');
const statsSuccessRate = document.getElementById('stats-success-rate');
const closeStatsBtn = document.getElementById('close-stats-btn');
const closeSessionBtn = document.getElementById('close-session-btn');
const addEditQuestionModal = new bootstrap.Modal(document.getElementById('addEditQuestionModal'));
const addEditQuestionFormElement = document.getElementById('add-edit-question-form');
// Load Q&A sets from localStorage
function loadQASets() {
const data = localStorage.getItem('qaSets');
if (data) {
qaSets = JSON.parse(data);
} else {
qaSets = [];
}
renderQASetsList();
}
// Save Q&A sets to localStorage
function saveQASets() {
localStorage.setItem('qaSets', JSON.stringify(qaSets));
}
// Render Q&A sets in the list
function renderQASetsList() {
qaSetsList.innerHTML = '';
qaSets.forEach((set, index) => {
const li = document.createElement('li');
li.className = 'list-group-item d-flex justify-content-between align-items-center';
li.innerHTML = `
<span>${set.setName}</span>
<div>
<button class="btn btn-sm btn-primary me-2" onclick="manageSet(${index})" title="Manage Set"><i class="fas fa-edit"></i></button>
<button class="btn btn-sm btn-success me-2" onclick="startSession(${index})" title="Start Session"><i class="fas fa-play"></i></button>
<button class="btn btn-sm btn-danger" onclick="deleteSet(${index})" title="Delete Set"><i class="fas fa-trash"></i></button>
</div>
`;
qaSetsList.appendChild(li);
});
}
// Add new Q&A set
addSetForm.addEventListener('submit', function(e) {
e.preventDefault();
const setName = document.getElementById('set-name').value.trim();
const qaInput = document.getElementById('qa-input').value.trim();
if (!setName || !qaInput) {
alert('Please provide both set name and Q&A data.');
return;
}
try {
const questions = JSON.parse(qaInput);
if (!Array.isArray(questions)) {
throw new Error('Q&A data should be an array of questions.');
}
// Initialize question properties if not present
questions.forEach(q => {
q.successRate = q.successRate || 0;
q.attempts = q.attempts || 0;
q.correct = q.correct || 0;
});
qaSets.push({ setName, questions });
saveQASets();
renderQASetsList();
addSetForm.reset();
const addSetModal = bootstrap.Modal.getInstance(document.getElementById('addSetModal'));
addSetModal.hide();
} catch (error) {
alert('Invalid JSON format. Please check your input.');
console.error(error);
}
});
// Manage a specific Q&A set
function manageSet(index) {
selectedSetIndex = index;
const set = qaSets[index];
manageSetName.textContent = set.setName;
renderQuestionsTable();
manageSetModal.show();
}
// Render questions in the manage set modal
function renderQuestionsTable() {
questionsTableBody.innerHTML = '';
const set = qaSets[selectedSetIndex];
set.questions.forEach((q, idx) => {
const tr = document.createElement('tr');
tr.innerHTML = `
<td>${idx + 1}</td>
<td>${q.question}</td>
<td>${q.answer}</td>
<td>${q.successRate.toFixed(2)}%</td>
<td>
<button class="btn btn-sm btn-secondary me-2" onclick="editQuestion(${idx})" title="Edit"><i class="fas fa-edit"></i></button>
<button class="btn btn-sm btn-danger" onclick="deleteQuestion(${idx})" title="Delete"><i class="fas fa-trash"></i></button>
</td>
`;
questionsTableBody.appendChild(tr);
});
}
// Delete a Q&A set
function deleteSet(index) {
if (confirm('Are you sure you want to delete this Q&A set?')) {
qaSets.splice(index, 1);
saveQASets();
renderQASetsList();
}
}
// Delete a question from a set
function deleteQuestion(qIndex) {
if (confirm('Are you sure you want to delete this question?')) {
qaSets[selectedSetIndex].questions.splice(qIndex, 1);
saveQASets();
renderQuestionsTable();
}
}
// Edit a question
function editQuestion(qIndex) {
const question = qaSets[selectedSetIndex].questions[qIndex];
document.getElementById('question-id').value = qIndex;
document.getElementById('question-text').value = question.question;
document.getElementById('answer-text').value = question.answer;
addEditQuestionModal.show();
}
// Add a new question
addQuestionBtn.addEventListener('click', function() {
document.getElementById('question-id').value = '';
document.getElementById('question-text').value = '';
document.getElementById('answer-text').value = '';
addEditQuestionModal.show();
});
// Handle add/edit question form submission
addEditQuestionForm.addEventListener('submit', function(e) {
e.preventDefault();
const qIndex = document.getElementById('question-id').value;
const questionText = document.getElementById('question-text').value.trim();
const answerText = document.getElementById('answer-text').value.trim();
if (!questionText || !answerText) {
alert('Please provide both question and answer.');
return;
}
if (qIndex === '') {
// Add new question
const newQuestion = {
id: qaSets[selectedSetIndex].questions.length + 1,
question: questionText,
answer: answerText,
successRate: 0,
attempts: 0,
correct: 0
};
qaSets[selectedSetIndex].questions.push(newQuestion);
} else {
// Edit existing question
const question = qaSets[selectedSetIndex].questions[qIndex];
question.question = questionText;
question.answer = answerText;
}
saveQASets();
renderQuestionsTable();
addEditQuestionModal.hide();
});
// Start a Q&A session
function startSession(index) {
selectedSetIndex = index;
const set = qaSets[index];
if (set.questions.length === 0) {
alert('This Q&A set has no questions.');
return;
}
// Populate session options
questionsSelect.innerHTML = '';
set.questions.forEach((q, idx) => {
const option = document.createElement('option');
option.value = idx;
option.textContent = q.question;
questionsSelect.appendChild(option);
});
sessionSetName.textContent = set.setName;
// Reset session views
sessionOptions.classList.remove('d-none');
sessionContent.classList.add('d-none');
sessionStats.classList.add('d-none');
sessionModal.show();
}
// Start session button click
startSessionBtn.addEventListener('click', function() {
const selectedOptions = Array.from(questionsSelect.selectedOptions).map(opt => parseInt(opt.value));
const order = questionOrder.value;
const shuffle = shuffleQuestions.value === 'yes';
let selectedQuestions = [];
if (selectedOptions.length === 0) {
// All questions
selectedQuestions = qaSets[selectedSetIndex].questions.map((q, idx) => idx);
} else {
selectedQuestions = selectedOptions;
}
// Order questions
if (order === 'successRate') {
selectedQuestions.sort((a, b) => qaSets[selectedSetIndex].questions[a].successRate - qaSets[selectedSetIndex].questions[b].successRate);
} else {
// Insertion order (already in order)
}
// Shuffle if needed
if (shuffle) {
for (let i = selectedQuestions.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[selectedQuestions[i], selectedQuestions[j]] = [selectedQuestions[j], selectedQuestions[i]];
}
}
sessionQuestions = selectedQuestions;
currentQuestionIndex = 0;
sessionCorrect = 0;
sessionWrong = 0;
// Hide options and show first question
sessionOptions.classList.add('d-none');
sessionContent.classList.remove('d-none');
sessionStats.classList.add('d-none');
showQuestion();
});
// Show current question
function showQuestion() {
if (currentQuestionIndex >= sessionQuestions.length) {
endSession();
return;
}
const qIndex = sessionQuestions[currentQuestionIndex];
const question = qaSets[selectedSetIndex].questions[qIndex];
currentQuestionNumber.textContent = currentQuestionIndex + 1;
totalQuestions.textContent = sessionQuestions.length;
currentQuestionText.textContent = question.question;
currentAnswerText.textContent = question.answer;
currentAnswerText.classList.add('d-none');
showAnswerBtn.classList.remove('d-none');
}
// Show answer button click
showAnswerBtn.addEventListener('click', function() {
currentAnswerText.classList.remove('d-none');
showAnswerBtn.classList.add('d-none');
});
// Mark correct
markCorrectBtn.addEventListener('click', function() {
recordAnswer(true);
});
// Mark wrong
markWrongBtn.addEventListener('click', function() {
recordAnswer(false);
});
// Record user's answer
function recordAnswer(isCorrect) {
const qIndex = sessionQuestions[currentQuestionIndex];
const question = qaSets[selectedSetIndex].questions[qIndex];
question.attempts += 1;
if (isCorrect) {
question.correct += 1;
sessionCorrect += 1;
} else {
sessionWrong += 1;
}
question.successRate = (question.correct / question.attempts) * 100;
saveQASets();
currentQuestionIndex += 1;
showQuestion();
}
// End session and show stats
function endSession() {
sessionContent.classList.add('d-none');
sessionStats.classList.remove('d-none');
statsTotal.textContent = sessionQuestions.length;
statsCorrect.textContent = sessionCorrect;
statsWrong.textContent = sessionWrong;
const successRate = sessionCorrect / sessionQuestions.length * 100;
statsSuccessRate.textContent = successRate.toFixed(2);
}
// Close stats and reset session modal
closeStatsBtn.addEventListener('click', function() {
sessionModal.hide();
});
// Prevent closing session modal with backdrop or escape key
// You can adjust this behavior as needed
// const sessionModalEl = document.getElementById('sessionModal');
// const mySessionModal = new bootstrap.Modal(sessionModalEl, {
// backdrop: 'static',
// keyboard: false
// });
// Initialize the application
loadQASets();
</script>
</body>
</html>
Features and Usage
-
Add Q&A Sets:
- Click the "Add Set" button to open the modal.
- Enter a Set Name.
- Input your Q&A data in JSON format. Ensure the JSON structure matches the defined format.
- Click "Add Set" to save the set.
-
Manage Q&A Sets:
- In the Q&A Sets list, each set has options to Manage, Start Session, or Delete.
- Click Manage (Edit) to view, add, edit, or delete questions within the set.
- In the Manage modal:
- Add Question: Click to open a form where you can add a new question and answer.
- Edit: Modify existing questions and answers.
- Delete: Remove questions from the set.
-
Start a Q&A Session:
- Click the Start Session (Play) button next to a Q&A set.
- In the Session modal:
- Select Questions: Choose specific questions to include or leave unselected to include all.
- Question Order: Choose to order by Insertion Order or Success Rate (lowest first).
- Shuffle Questions: Decide whether to shuffle the questions.
- Click Start Session to begin.
- During the session:
- Questions are presented one by one.
- Click Show Answer to reveal the answer.
- After reviewing the answer, mark as Correct or Wrong.
- Upon completion, session statistics are displayed, including total questions, correct answers, wrong answers, and overall success rate.
-
Data Persistence:
- The application uses localStorage to save Q&A sets and progress. This ensures that your data remains intact across browser sessions.
-
Responsive Design:
- The UI is built with Bootstrap 5, ensuring it is responsive and works well on various devices and screen sizes.
Customization and Enhancements
You can further enhance the application by considering the following:
- Export/Import Functionality: Allow users to export their Q&A sets as JSON files and import them later.
- Search and Filter: Implement search functionality within Q&A sets to quickly find questions.
- Categories or Tags: Organize questions using categories or tags for better management.
- User Authentication: Implement user authentication to allow multiple users to have their own Q&A sets.
- Advanced Statistics: Provide more detailed analytics, such as tracking progress over time.
- Progressive Web App (PWA): Convert the application into a PWA for offline access and mobile device installation.
Conclusion
The provided application offers a robust and user-friendly platform for managing and conducting Q&A sessions. The use of JSON for data input ensures flexibility and ease of data management, while the Bootstrap-powered UI guarantees a pleasant user experience. Feel free to customize and expand upon this foundation to suit your specific needs.