-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.js
36 lines (30 loc) · 888 Bytes
/
main.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
function submitAnswers(){
var total = 5;
var score = 0;
// Get User Input
var q1 = document.forms["quizForm"]["q1"].value;
var q2 = document.forms["quizForm"]["q2"].value;
var q3 = document.forms["quizForm"]["q3"].value;
var q4 = document.forms["quizForm"]["q4"].value;
var q5 = document.forms["quizForm"]["q5"].value;
// Validation
for(i = 1; i <= total;i++){
if(eval('q'+i) == null || eval('q'+i) == ''){
alert('You missed question '+ i);
return false;
}
}
// Set Correct Answers
var answers = ["b","a","d","b","d"];
// Check Answers
for(i = 1; i <= total;i++){
if(eval('q'+i) == answers[i - 1]){
score++;
}
}
// Display Results
var results = document.getElementById('results');
results.innerHTML = '<h3>You scored <span>'+score+'</span> out of <span>'+total+'</span></h3>';
alert('You scored '+score+' out of ' +total);
return false;
}