Learn to build an online quiz app with Lovable. Follow our step-by-step guide featuring code samples, expert tips, and best practices for an engaging experience.
Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
Creating a New Lovable Project
Setting Up the File Structure
index.html
in the root directory. This file will contain the HTML markup for your quiz app.style.css
for styling.quiz.js
which will contain the JavaScript logic for the quiz functionality.
Creating the User Interface
index.html
and add the following code snippet. This will set up the basic structure and include references to your CSS and JavaScript files:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Online Quiz App</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div id="quiz-container">
<h1>Online Quiz App</h1>
<div id="question"></div>
<ul id="choices"></ul>
<button id="next-btn">Next Question</button>
</div>
<script src="quiz.js"></script>
</body>
</html>
Styling the Quiz App
style.css
and add the following CSS code snippet. This will style your quiz container, list items, and overall layout:
body {
font-family: Arial, sans-serif;
background-color: #f4f4f4;
text-align: center;
margin: 0;
padding: 0;
}
#quiz-container {
background-color: #fff;
padding: 20px;
margin: 50px auto;
max-width: 500px;
border-radius: 8px;
box-shadow: 0 0 10px rgba(0, 0, 0, 0.1);
}
ul {
list-style: none;
padding: 0;
}
li {
background: #e2e2e2;
margin: 10px 0;
padding: 10px;
cursor: pointer;
border-radius: 4px;
}
li:hover {
background: #d4d4d4;
}
button {
padding: 10px 20px;
margin-top: 20px;
border: none;
border-radius: 4px;
background-color: #333;
color: #fff;
cursor: pointer;
}
button:hover {
background-color: #555;
}
Implementing Quiz Logic in JavaScript
quiz.js
and add the following JavaScript code snippet. This script defines your quiz questions, handles the display of questions and choices, processes user selection, and shows the final result:
const quizData = [
{
question: "What is the capital of France?",
choices: ["Berlin", "Madrid", "Paris", "Lisbon"],
correct: "Paris"
},
{
question: "Who wrote 'Hamlet'?",
choices: ["Charles Dickens", "William Shakespeare", "Leo Tolstoy", "Mark Twain"],
correct: "William Shakespeare"
}
];
let currentQuestion = 0;
let score = 0;
const questionEl = document.getElementById('question');
const choicesEl = document.getElementById('choices');
const nextBtn = document.getElementById('next-btn');
function loadQuestion() {
const qData = quizData[currentQuestion];
questionEl.textContent = qData.question;
choicesEl.innerHTML = "";
qData.choices.forEach(choice => {
const li = document.createElement('li');
li.textContent = choice;
li.addEventListener('click', () => selectAnswer(choice, li));
choicesEl.appendChild(li);
});
}
function selectAnswer(selected, element) {
const qData = quizData[currentQuestion];
if (selected === qData.correct) {
element.style.backgroundColor = 'lightgreen';
score++;
} else {
element.style.backgroundColor = 'lightcoral';
}
// Disable all choices after selection
Array.from(choicesEl.children).forEach(li => {
li.style.pointerEvents = 'none';
});
}
nextBtn.addEventListener('click', () => {
currentQuestion++;
if (currentQuestion < quizData.length) {
loadQuestion();
} else {
showResults();
}
});
function showResults() {
questionEl.textContent = Quiz Completed! Your score is ${score} out of ${quizData.length}.
;
choicesEl.innerHTML = "";
nextBtn.style.display = 'none';
}
// Load the first question when the page loads
loadQuestion();
Handling Dependencies Without a Terminal
index.html
before your custom quiz.js
reference. For example, to include a library from a CDN:
<script src="https://cdn.example.com/library.js"></script>
Previewing and Testing Your App
index.html
, styled by style.css
, and powered by quiz.js
.
Online Quiz API Example
Submit Quiz Score to Leaderboard
Lovable Quiz Real-time Sync
Waiting for the next question...
Timer: --
Book a call with an Expert
Starting a new venture? Need to upgrade your web app? RapidDev builds application with your growth in mind.
Understanding the Project and Its Goals
Prerequisites
Designing the Quiz App Concept
Selecting a Development Approach
Setting Up Your Development Environment
Building the Front-End of the Quiz App
function loadQuizQuestion(questionData) {
document.getElementById('question').innerText = questionData.question;
const answersContainer = document.getElementById('answers');
answersContainer.innerHTML = '';
questionData.answers.forEach(function(answer) {
let btn = document.createElement('button');
btn.innerText = answer;
btn.onclick = function() {
checkAnswer(answer, questionData.correctAnswer);
};
answersContainer.appendChild(btn);
});
}
Creating the Back-End to Serve Quiz Data
const express = require('express');
const app = express();
const PORT = process.env.PORT || 3000;
app.use(express.json());
// Sample quiz question endpoint
app.get('/api/quiz', (req, res) => {
const question = {
question: "What is the capital of France?",
answers: ["Paris", "Rome", "Madrid", "Berlin"],
correctAnswer: "Paris"
};
res.json(question);
});
app.listen(PORT, () => {
console.log(`Server is running on port ${PORT}`);
});
Integrating AI Code Generators
Implementing Quiz Scoring and User Feedback
function checkAnswer(selectedAnswer, correctAnswer) {
if (selectedAnswer === correctAnswer) {
alert("Correct!");
// update score logic here
} else {
alert("Incorrect. Try the next question!");
}
}
Testing Your Quiz App
When it comes to serving you, we sweat the little things. That’s why our work makes a big impact.