<div class="container">
<div id="quizScreen">
<div class="header">
<h1>JavaScript Quiz</h1>
<div class="timer" id="timer">30</div>
</div>
<div class="progress-bar">
<div class="progress-fill" id="progressBar"></div>
</div>
<div class="question-container">
<div class="question-number" id="questionNumber">Question 1 of 5</div>
<div class="question" id="question"></div>
<div class="options" id="options"></div>
</div>
<button class="button" id="nextBtn" disabled>Next Question</button>
</div>
<div id="resultsScreen" class="results hidden">
<h2>Quiz Complete!</h2>
<div class="score" id="scoreDisplay"></div>
<div class="stats">
<div class="stat">
<div class="stat-value" id="correctCount">0</div>
<div class="stat-label">Correct</div>
</div>
<div class="stat">
<div class="stat-value" id="incorrectCount">0</div>
<div class="stat-label">Incorrect</div>
</div>
<div class="stat">
<div class="stat-value" id="timeSpent">0s</div>
<div class="stat-label">Time Spent</div>
</div>
</div>
<button class="button" id="restartBtn">Restart Quiz</button>
</div>
</div>
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, Cantarell, sans-serif;
background: linear-gradient(135deg, #667eea 0%, #00d4ff 50%, #0093e9 100%);
min-height: 100vh;
display: flex;
justify-content: center;
align-items: center;
padding: 20px;
}
.container {
background: white;
border-radius: 16px;
box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3);
padding: 40px;
max-width: 700px;
width: 100%;
}
.header {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 32px;
}
h1 {
font-size: 28px;
color: #333;
font-weight: 600;
}
.timer {
display: flex;
align-items: center;
gap: 8px;
padding: 12px 20px;
background: linear-gradient(135deg, #0093e9 0%, #00d4ff 100%);
color: white;
border-radius: 8px;
font-size: 20px;
font-weight: 600;
min-width: 100px;
justify-content: center;
}
.timer.warning {
background: linear-gradient(135deg, #ff6b6b 0%, #ff8e53 100%);
animation: pulse 1s infinite;
}
@keyframes pulse {
0%, 100% { transform: scale(1); }
50% { transform: scale(1.05); }
}
.progress-bar {
height: 8px;
background: #e0e0e0;
border-radius: 4px;
margin-bottom: 32px;
overflow: hidden;
}
.progress-fill {
height: 100%;
background: linear-gradient(90deg, #0093e9 0%, #00d4ff 100%);
transition: width 0.3s ease;
border-radius: 4px;
}
.question-container {
margin-bottom: 32px;
}
.question-number {
color: #0093e9;
font-size: 14px;
font-weight: 600;
margin-bottom: 12px;
}
.question {
font-size: 20px;
color: #333;
margin-bottom: 24px;
line-height: 1.5;
}
.options {
display: flex;
flex-direction: column;
gap: 12px;
}
.option {
padding: 16px 20px;
border: 2px solid #e0e0e0;
border-radius: 8px;
cursor: pointer;
transition: all 0.3s ease;
background: white;
font-size: 16px;
color: #333;
}
.option:hover {
border-color: #0093e9;
background: #f0f9ff;
transform: translateX(4px);
}
.option.selected {
border-color: #0093e9;
background: linear-gradient(135deg, #0093e9 0%, #00d4ff 100%);
color: white;
}
.option.correct {
border-color: #00c853;
background: #00c853;
color: white;
}
.option.incorrect {
border-color: #ff6b6b;
background: #ff6b6b;
color: white;
}
.option.disabled {
cursor: not-allowed;
opacity: 0.6;
}
.button {
padding: 14px 32px;
background: linear-gradient(135deg, #0093e9 0%, #00d4ff 100%);
color: white;
border: none;
border-radius: 8px;
font-size: 16px;
font-weight: 600;
cursor: pointer;
transition: transform 0.2s, box-shadow 0.2s;
width: 100%;
}
.button:hover {
transform: translateY(-2px);
box-shadow: 0 4px 12px rgba(0, 147, 233, 0.3);
}
.button:active {
transform: translateY(0);
}
.button:disabled {
opacity: 0.5;
cursor: not-allowed;
transform: none;
}
.results {
text-align: center;
padding: 40px 0;
}
.results h2 {
font-size: 32px;
color: #333;
margin-bottom: 16px;
}
.score {
font-size: 48px;
font-weight: 700;
background: linear-gradient(135deg, #0093e9 0%, #00d4ff 100%);
-webkit-background-clip: text;
-webkit-text-fill-color: transparent;
background-clip: text;
margin-bottom: 24px;
}
.stats {
display: grid;
grid-template-columns: repeat(3, 1fr);
gap: 16px;
margin-bottom: 32px;
}
.stat {
padding: 16px;
background: #f8f9fa;
border-radius: 8px;
}
.stat-value {
font-size: 24px;
font-weight: 600;
color: #333;
margin-bottom: 4px;
}
.stat-label {
font-size: 13px;
color: #666;
}
.hidden {
display: none;
}
const questions = [
{
question: "What is the correct way to declare a variable in JavaScript?",
options: ["variable x = 5", "let x = 5", "var x == 5", "x := 5"],
correct: 1
},
{
question: "Which method is used to add an element at the end of an array?",
options: ["push()", "pop()", "shift()", "unshift()"],
correct: 0
},
{
question: "What does 'NaN' stand for in JavaScript?",
options: ["Null and Null", "Not a Number", "New Array Number", "Negative Array Number"],
correct: 1
},
{
question: "Which operator is used for strict equality in JavaScript?",
options: ["=", "==", "===", "!="],
correct: 2
},
{
question: "What is the output of: typeof []?",
options: ["array", "object", "list", "undefined"],
correct: 1
}
];
let currentQuestion = 0;
let score = 0;
let selectedAnswer = null;
let timeLeft = 30;
let timerInterval = null;
let totalTime = 0;
const timer = document.getElementById('timer');
const progressBar = document.getElementById('progressBar');
const questionNumber = document.getElementById('questionNumber');
const questionEl = document.getElementById('question');
const optionsEl = document.getElementById('options');
const nextBtn = document.getElementById('nextBtn');
const quizScreen = document.getElementById('quizScreen');
const resultsScreen = document.getElementById('resultsScreen');
function startTimer() {
timeLeft = 30;
timer.classList.remove('warning');
timerInterval = setInterval(() => {
timeLeft--;
totalTime++;
timer.textContent = timeLeft;
if (timeLeft <= 10) {
timer.classList.add('warning');
}
if (timeLeft === 0) {
clearInterval(timerInterval);
handleTimeout();
}
}, 1000);
}
function handleTimeout() {
Array.from(optionsEl.children).forEach(opt => {
opt.classList.add('disabled');
});
nextBtn.disabled = false;
}
function loadQuestion() {
const q = questions[currentQuestion];
questionNumber.textContent = `Question ${currentQuestion + 1} of ${questions.length}`;
questionEl.textContent = q.question;
optionsEl.innerHTML = '';
q.options.forEach((option, index) => {
const div = document.createElement('div');
div.className = 'option';
div.textContent = option;
div.onclick = () => selectAnswer(index);
optionsEl.appendChild(div);
});
updateProgress();
startTimer();
selectedAnswer = null;
nextBtn.disabled = true;
}
function selectAnswer(index) {
if (selectedAnswer !== null) return;
selectedAnswer = index;
clearInterval(timerInterval);
const options = Array.from(optionsEl.children);
options.forEach((opt, i) => {
opt.classList.add('disabled');
if (i === questions[currentQuestion].correct) {
opt.classList.add('correct');
} else if (i === index) {
opt.classList.add('incorrect');
}
});
if (index === questions[currentQuestion].correct) {
score++;
}
nextBtn.disabled = false;
}
function updateProgress() {
const progress = ((currentQuestion + 1) / questions.length) * 100;
progressBar.style.width = progress + '%';
}
function showResults() {
quizScreen.classList.add('hidden');
resultsScreen.classList.remove('hidden');
const percentage = Math.round((score / questions.length) * 100);
document.getElementById('scoreDisplay').textContent = percentage + '%';
document.getElementById('correctCount').textContent = score;
document.getElementById('incorrectCount').textContent = questions.length - score;
document.getElementById('timeSpent').textContent = totalTime + 's';
}
function restartQuiz() {
currentQuestion = 0;
score = 0;
totalTime = 0;
resultsScreen.classList.add('hidden');
quizScreen.classList.remove('hidden');
loadQuestion();
}
nextBtn.addEventListener('click', () => {
currentQuestion++;
if (currentQuestion < questions.length) {
loadQuestion();
} else {
showResults();
}
});
document.getElementById('restartBtn').addEventListener('click', restartQuiz);
loadQuestion();
No comments yet. Be the first!