-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.js
166 lines (134 loc) · 3.21 KB
/
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
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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
window.Quizzed = (function( $ ){
var obj = {};
var score = {};
var counter = 0;
var questions = [];
var types = {};
var typeArr = [];
var facebookShareArgs = {};
obj.init = function( args ){
data = args;
questions = args.questions;
types = args.types;
obj.findTeams();
obj.draw( counter );
}
obj.findTeams = function(){
var qCount = questions.length;
for( var i = 0; i < qCount; i++ ){
var answers = questions[i].answers;
var aCount = answers.length;
for( var t = 0; t < aCount; t++ ){
var types = answers[t].type;
if( typeof types == 'string' ){
types = [ types ];
}
var tCount = types.length;
for( var m = 0; m < tCount; m++ ){
if( typeof score[types[m] ] == 'undefined' ){
score[ types[m] ] = 0;
typeArr.push( types[m] );
}
}
}
}
}
obj.clickHandler = function(){
var type = $(this).data( 'type' );
if( type.indexOf(',') ){
type = type.split(',');
}else{
type = [ type ];
}
var count = type.length;
for( var i = 0; i < count; i++ ){
score[ type[i] ] = score[ type[i] ] + 1;
}
counter = counter + 1;
if( counter == questions.length ){
obj.finishGame();
}else{
obj.draw( counter );
}
}
obj.draw = function( number ){
var main = $("#quizzed");
main.html("");
var question = questions[ number ].question;
var answers = questions[ number ].answers;
$( '<div/>', {
'class' : 'question',
'text' : question
}).appendTo(main);
answers = shuffleArray( answers );
var count = answers.length;
for( var i = 0; i < count; i++ ){
var item = answers[i];
$( '<div/>', {
'class' : 'answer',
'data-type' : item.type,
'html' : item.text,
'click' : obj.clickHandler,
}).appendTo(main);
}
}
obj.finishGame = function(){
var main = $("#quizzed");
main.html("");
names = shuffleArray( typeArr );
var topScore = 0;
var winner = '';
var count = names.length;
for( var i = 0; i < count; i++ ){
var number = score[ names[i] ];
console.log( number );
if( number > topScore ){
topScore = number;
winner = names[i];
}
}
var winner = types[winner];
$( '<img/>', {
'class' : 'winner-img',
'src' : winner.img,
} ).appendTo(main);
$( '<div/>', {
'class' : 'winner-introduction',
'html' : 'You are most similar to...',
} ).appendTo(main);
$( '<div/>', {
'class' : 'winner',
'html' : winner.name,
} ).appendTo(main);
$( '<div/>', {
'class' : 'winner-desc',
'html' : winner.desc,
} ).appendTo(main);
$( '<a/>', {
'click' : obj.fbShare,
'text' : 'Share on Facebook',
'class' : 'facebook-share',
}).appendTo(main);
facebookShareArgs = {
method: 'feed',
name: 'Who are you at GPC?',
picture: 'http://projects.102content.com/embed/gpc/' + winner.img,
link: 'http://gpcstudents.com/quiz',
caption: 'You got ' + winner.name + '!!!',
description: winner.desc
};
}
obj.fbShare = function(){
FB.ui( facebookShareArgs );
}
function shuffleArray(array) {
for (var i = array.length - 1; i > 0; i--) {
var j = Math.floor(Math.random() * (i + 1));
var temp = array[i];
array[i] = array[j];
array[j] = temp;
}
return array;
}
return obj;
})(jQuery);