forked from akshitagupta15june/Moksh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
story.js
42 lines (36 loc) · 1.1 KB
/
story.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
const quizForm = document.querySelector('#quiz-form');
const submitButton = document.querySelector('#submit-button');
const resultsDiv = document.querySelector('#results');
submitButton.addEventListener('click', (event) => {
event.preventDefault();
const userAnswers = [];
let score = 0;
// get user answers and calculate score
const question1Answer = quizForm.elements.question1.value;
userAnswers.push(question1Answer);
if (question1Answer === 'a') {
score++;
}
const question2Answer = quizForm.elements.question2.value;
userAnswers.push(question2Answer);
if (question2Answer === 'a') {
score++;
}
const question3Answer = quizForm.elements.question3.value;
userAnswers.push(question3Answer);
if (question3Answer === 'c') {
score++;
}
// display results
let resultText = `You scored ${score} out of 3.<br>`;
for (let i = 0; i < userAnswers.length; i++) {
const questionNumber = i + 1;
resultText += `Question ${questionNumber}: `;
if (userAnswers[i] === 'a') {
resultText += `Correct.<br>`;
} else {
resultText += `Incorrect.<br>`;
}
}
resultsDiv.innerHTML = resultText;
});