-
Notifications
You must be signed in to change notification settings - Fork 1
/
quiz-1.js
55 lines (47 loc) · 1.94 KB
/
quiz-1.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
$(document).ready(function() {
_.templateSettings = {
interpolate: /\{\{(.+?)\}\}/g
};
var template = _.template('<div class="alert alert-warning" role="alert">' +
'<strong>Question {{number}}: </strong>{{tip}}</div>');
$('#results').on('click', function() {
$('#categorylist').empty();
$('#alerts').empty();
var questions = [
{ number: 1, answer: 'a', tip: 'Be smarter!'},
{ number: 2, answer: 'b', tip: 'Be smarter!'},
{ number: 3, answer: 'c', tip: 'Be smarter!'},
{ number: 4, answer: 'd', tip: 'Be smarter!'},
{ number: 5, answer: 'a', tip: 'Be smarter!'},
{ number: 6, answer: 'b', tip: 'Be smarter!'},
{ number: 7, answer: 'c', tip: 'Be smarter!'},
{ number: 8, answer: 'd', tip: 'Be smarter!'},
{ number: 9, answer: 'a', tip: 'Be smarter!'},
{ number: 10, answer: 'b', tip: 'Be smarter!'}
];
var all_answered = function() {
return _.every(_.pluck(questions, 'number'), function(i) {
return $('input[name="q' + i + '"]:checked').val();
});
};
if (! all_answered()) {
$('#alerts').append('<div class="alert alert-danger" role="alert">You\'re not done yet!</div>');
} else {
var incorrect = _.reject(questions, function(e) {
return $('input[name="q' + e.number + '"]:checked').val() === e.answer;
});
var catStr = '';
if (incorrect.length === 0) {
$('#alerts').append('<div class="alert alert-success" role="alert">You got everything correct! Congrats</div>');
//catStr = 'You got everything correct! Congrats';
} else {
$('#alerts').append('<div class="alert alert-danger" role="alert">' +
'You answered the following questions incorrectly: ' +
_.pluck(incorrect, 'number').join(', ') + '</div>');
}
_.each(incorrect, function(e) {
$('#alerts').append(template(e));
});
}
});
});