forked from Rounak200316/cse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
64 lines (52 loc) · 1.98 KB
/
app.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
const questions = [
{
question: "What is AI?",
options: ["Artificial Intelligence", "Actual Intelligence", "Automated Intelligence"],
answer: "Artificial Intelligence",
difficulty: 1
},
// Add more questions with options and answers, along with difficulty levels
];
let currentQuestionIndex = 0;
let score = 0;
function displayQuestion() {
const questionElement = document.getElementById('question');
const optionsElement = document.getElementById('options');
questionElement.innerHTML = questions[currentQuestionIndex].question;
optionsElement.innerHTML = '';
questions[currentQuestionIndex].options.forEach(option => {
optionsElement.innerHTML += <input type="radio" name="option" value="${option}">${option}<br></br></input>;
});
}
function checkAnswer() {
const selectedOption = document.querySelector('input[name="option"]:checked');
if (!selectedOption) {
alert('Please select an option.');
return;
}
if (selectedOption.value === questions[currentQuestionIndex].answer) {
score++;
if (questions[currentQuestionIndex].difficulty < 3) {
questions[currentQuestionIndex].difficulty++;
}
} else {
if (questions[currentQuestionIndex].difficulty > 1) {
questions[currentQuestionIndex].difficulty--;
}
}
currentQuestionIndex++;
if (currentQuestionIndex < questions.length) {
displayQuestion();
}
else {
showResult();
}
}
function showResult() {
const quizContainer = document.querySelector('.quiz-container');
const resultContainer = document.querySelector('.result-container');
const resultElement = document.getElementById('result');
quizContainer.style.display = 'none';
resultContainer.style.display = 'block';
resultElement.innerHTML = "You scored ${score} outof ${questions.length}";
}