-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
118 lines (109 loc) · 3.55 KB
/
index.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
var quizData = [
{
question: "Inside which HTML element do we put the JavaScript?",
A: "<javascript>",
B: "<js>",
C: "<script>",
D: "<scripting>",
answer: "optionC"
},
{
question: "Which built-in method returns the length of the string?",
A: "length()",
B: "size()",
C: "index()",
D: "None of the Above",
answer: "optionA"
},
{
question: "Which built-in method returns the calling string value converted to lower case?",
A: "toLowerCase",
B: "toLower()",
C: "changeCase()",
D: "None of the above",
answer: "optionA"
},
{
question: "Which of the following function of Number object returns a string value version of the current number?",
A: "toFixed()",
B: "toLocaleString()",
C: "toString()",
D: "toPrecision()",
answer: "optionC"
},
{
question: "Which of the following function of String object causes a string to be displayed in a small font, as if it were in a <small> tag?",
A: "link()",
B: "small()",
C: "sup()",
D: "sub()",
answer: "optionB"
},
{
question: "Which of the following function of Array object extracts a section of an array and returns a new array?",
A: "reverse()",
B: "shift()",
C: "slice()",
D: "some()",
answer: "optionC"
}
]
var quiz = document.getElementById("quiz");
var options = document.querySelectorAll(".options");
var question = document.getElementById("question");
var optionA = document.getElementById("optionAValue");
var optionB = document.getElementById("optionBValue");
var optionC = document.getElementById("optionCValue");
var optionD = document.getElementById("optionDValue");
var submitbtn = document.getElementById("submitbtn");
var currentQuestion = 0;
var QuizScore = 0;
var remarks;
loadQuiz();
function loadQuiz(){
deselect();
question.innerText = quizData[currentQuestion].question;
optionA.innerText = quizData[currentQuestion].A;
optionB.innerText = quizData[currentQuestion].B;
optionC.innerText = quizData[currentQuestion].C;
optionD.innerText = quizData[currentQuestion].D;
}
function deselect(){
options.forEach(options=>options.checked=false)
}
submitbtn.addEventListener('click',()=>{
var selectedOption;
options.forEach(options=>{
if(options.checked){
selectedOption = options.id;
}
})
if(selectedOption == quizData[currentQuestion].answer ){
QuizScore = QuizScore + 1;
}
currentQuestion = currentQuestion + 1;
if(currentQuestion == quizData.length-1){
submitbtn.innerText = "SUBMIT";
}
if(currentQuestion == quizData.length){
if(QuizScore == 6){
remarks = "Outstanding!";
}
else if(QuizScore == 5){
remarks = "Excellent";
}
else if(QuizScore == 4){
remarks = "Good";
}
else if(QuizScore == 3){
remarks = "Average";
}
else{
remarks = "Work Hard";
}
document.getElementById('quizdiv').innerHTML = `<h2 class="my-3">Your Score: ${QuizScore}/${quizData.length}</h2><h4>You have answered ${QuizScore} questions correctly out of ${quizData.length} questions.</h4> <h4 class="text-center my-3">Remarks: ${remarks}</h4>`
}
else{
loadQuiz();
}
})