forked from ANKAN-MUKHOPADHYAY/JavaScript_QUIZ
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathqs.js
More file actions
109 lines (94 loc) · 4.87 KB
/
Copy pathqs.js
File metadata and controls
109 lines (94 loc) · 4.87 KB
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
window.onload = function () {
if (sessionStorage.getItem('LoggedInUser')== undefined || sessionStorage.getItem('LoggedInUser') == null ){
window.location.href='index.html'
}
var obj = new Object();
if(sessionStorage.getItem('questionSet') == 'html'){
obj.questionset ='htmlquestionset';
obj.questionsetHome = 'data/htmlquestionset.json';
} else if(sessionStorage.getItem('questionSet') == 'ajax'){
obj.questionset ='ajaxquestionset';
obj.questionsetHome = 'data/ajaxquestionset.json';
} else if(sessionStorage.getItem('questionSet') == 'bootstrap'){
obj.questionset ='bootstrapquestionset';
obj.questionsetHome = 'data/bootstrapquestionset.json';
} else if(sessionStorage.getItem('questionSet') == 'javascript'){
obj.questionset ='javascriptquestionset';
obj.questionsetHome = 'data/javascriptquestionset.json';
}
//ajaxProcedure(obj);
ajaxProcedureHome(obj);
};
var quizQuestion = '';
var count = 0;
var correctAns = 0;
function ajaxProcedure(a){
var y = new XMLHttpRequest();
y.onreadystatechange = function() {
if (this.readyState == 4) {
var quizQuestion = JSON.parse(this.responseText);
window.sessionStorage.setItem('ques', JSON.stringify(quizQuestion.questions));
showQues(quizQuestion.questions);
}
};
y.open('POST', 'http://192.168.1.2:7000/ideology/examsets', true);
y.setRequestHeader("Content-Type", "application/json");
y.send(JSON.stringify(a));
}
function ajaxProcedureHome(a){
console.log(a);
var y = new XMLHttpRequest();
y.onreadystatechange = function() {
if (this.readyState == 4) {
var quizQuestion = JSON.parse(this.responseText);
window.sessionStorage.setItem('ques', JSON.stringify(quizQuestion.questions));
showQues(quizQuestion.questions);
}
};
y.open('GET', a.questionsetHome, true);
y.setRequestHeader("Content-Type", "application/json");
y.send();
}
function showQues(data){
var div ="<div class='container'>";
var structure='<div><h2><p>'+sessionStorage.getItem('questionSet')+' Questions</p></h2>';
var Questdata='';
if(data[count].choices.length == 2){
Questdata +='<h5>'+data[count].question+ '</h5>' + '<ul><li><input type="radio" class="" name="option" value='+0+'>' + data[count].choices[0] + '</li><li><input type="radio" class="" name="option" value='+1+'>' + data[count].choices[1] + '</li></ul>' + '<button class="btn btn-success chkBtn" onclick="checkAnswer();">Check Answer</button><button class="btn btn-primary nextBtn" onclick="next();">Next</button>' + '<hr>';
} else if(data[count].choices.length == 3){
Questdata +='<h5>'+data[count].question+ '</h5>' + '<ul><li><input type="radio" class="" name="option" value='+0+'>' + data[count].choices[0] + '</li><li><input type="radio" class="" name="option" value='+1+'>' + data[count].choices[1] + '</li><li><input type="radio" class="" name="option" value='+2+'>' + data[count].choices[2] + '</li></ul>' + '<button class="btn btn-success chkBtn" onclick="checkAnswer();">Check Answer</button><button class="btn btn-primary nextBtn" onclick="next();">Next</button>' + '<hr>';
} else {
Questdata +='<h5>'+data[count].question+ '</h5>' + '<ul><li><input type="radio" class="" name="option" value='+0+'>' + data[count].choices[0] + '</li><li><input type="radio" class="" name="option" value='+1+'>' + data[count].choices[1] + '</li><li><input type="radio" class="" name="option" value='+2+'>' + data[count].choices[2] + '</li><li><input type="radio" class="" name="option" value='+3+'>' + data[count].choices[3] + '</li></ul>' + '<button class="btn btn-success chkBtn" onclick="checkAnswer();">Check Answer</button><button class="btn btn-primary nextBtn" onclick="next();">Next</button>' + '<hr>';
}
var finaldata = div + structure + Questdata + '</div><div id="ans"></div></div>';
document.getElementById('divcontenet').innerHTML= finaldata;
}
function checkAnswer(){
//var radioinput = document.querySelector('input[name="option"]:checked').value;
var radioinput = $('input[name="option"]:checked').val();
console.log(radioinput);
var answers = JSON.parse(window.sessionStorage.getItem('ques'));
//document.getElementsByClassName( 'chkBtn' ).style.display = 'none';
$('.chkBtn').hide();
$('.nextBtn').show();
console.log('-------------------------------------------');
console.log(answers[count].correct);
console.log(radioinput);
console.log('-------------------------------------------');
if(answers[count].correct === radioinput){
$('#ans').text('Wow Correct Answer');
correctAns++;
} else {
$('#ans').text('Sorry Wrong Answer');
}
}
function next(){
var questionset = JSON.parse(window.sessionStorage.getItem('ques'));
if(count == questionset.length-1){
$('.container').html('<h1>Wow This is Your Result: '+correctAns*100/questionset.length+'% </h1>');
//alert('Your Score : '+ correctAns);
} else {
count++;
showQues(questionset);
}
}