-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.js
More file actions
209 lines (180 loc) · 4.73 KB
/
Copy pathgame.js
File metadata and controls
209 lines (180 loc) · 4.73 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
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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
const question = document.getElementById('question');
const choices = Array.from(document.getElementsByClassName('choice-text'));
const progressText = document.getElementById('progressText');
const progressBarfull = document.getElementById("progressBarfull");
let currentQuestion = {};
let acceptingAnswers = false;
let score = 0;
let moralScore = 0;
let ethicScore = 0;
let questionCounter = 0;
let availableQuesions = [];
let questions = [
{
id: 0,
question: 'Try to follow the rules.',
item: 'X150',
axis: 'Law',
key: 1,
AB5C: 'II+III+',
category: 2
},
{
id: 1,
question: 'Respect authority.',
item: 'H296',
axis: 'Law',
key: 1,
AB5C: 'II+III+',
category: 2
},
{
id: 2,
question: 'Break rules.',
item: 'H1346',
axis: 'Law',
key: -1,
AB5C: 'II+III+',
category: 2
},
{
id: 3,
question: 'Do things by the book.',
item: 'H250',
axis: 'Law',
key: 1,
AB5C: 'III+V-',
category: 2
},
{
id: 4,
question: 'Stick to the rules.',
item: 'H294',
axis: 'Law',
key: 1,
AB5C: 'III+II+',
category: 2
},
{
id: 5,
question: 'Am concerned about others.',
item: 'H1100',
axis: 'Good',
key: 1,
AB5C: 'II+III-',
category: 1
},
{
id: 6,
question: 'Sympathize with others\' feelings.',
item: 'H1130',
axis: 'Good',
key: 1,
AB5C: 'II+II+',
category: 1
},
{
id: 7,
question: 'Am indifferent to the feelings of others.',
item: 'X203',
axis: 'Good',
key: -1,
AB5C: 'II+II+',
category: 1
},
{
id: 8,
question: 'Feel little concern for others.',
item: 'X244',
axis: 'Good',
key: -1,
AB5C: 'II+II+',
category: 1
},
{
id: 9,
question: 'Am not interested in other people\'s problems.',
item: 'X227',
axis: 'Good',
key: -1,
AB5C: 'II+II+',
category: 1
}
];
//CONSTANTS
const MAX_QUESTIONS = questions.length;
var questionIndex = 0;
var currentQuestionId = 0;
startGame = () => {
questionCounter = 0;
score = 0;
availableQuesions = [...questions]; /* to get a full copy*/
getNewQuestion();
};
getNewQuestion = () => {
if (availableQuesions.length === 0 || questionCounter >= MAX_QUESTIONS) {
localStorage.setItem('mostRecentMoralScore', moralScore);
localStorage.setItem('mostRecentEthicScore', ethicScore);
localStorage.setItem('questions', JSON.stringify(questions));
//go to the end page
return window.location.assign('./end.html');
}
questionCounter++;
progressText.innerText = "Question: " + questionCounter + "/" + MAX_QUESTIONS;
//update the progress bar
progressBarfull.style.width = `${(questionCounter / MAX_QUESTIONS) * 100}%`;
questionIndex = Math.floor(Math.random() * availableQuesions.length);
currentQuestion = availableQuesions[questionIndex];
currentQuestionId = currentQuestion.id;
question.innerText = "I " + currentQuestion.question.toLowerCase();
/* choices never change
choices.forEach((choice) => {
const number = choice.dataset['number'];
choice.innerText = currentQuestion['choice' + number];
});
*/
availableQuesions.splice(questionIndex, 1);
acceptingAnswers = true;
};
choices.forEach(choice => {
choice.addEventListener("click", e => {
if (!acceptingAnswers) return;
acceptingAnswers = false;
const selectedChoice = e.target;
const selectedAnswer = selectedChoice.dataset["number"];
//change answer in questions
questions[currentQuestionId].answer = selectedAnswer;
/*
No wrong answers
const classToApply =
selectedAnswer == currentQuestion.answer ? "correct" : "incorrect";
*/
incrementScore(parseInt(selectedAnswer));
selectedChoice.parentElement.classList.add("correct");
setTimeout(() => {
selectedChoice.parentElement.classList.remove("correct");
getNewQuestion();
}, 500);
});
});
incrementScore = num => {
currentCategory = currentQuestion.category;
currentKey = currentQuestion.key;
if (currentCategory == 1) {
if (currentKey == 1) {
moralScore += num;
}
if (currentKey == -1) {
moralScore += (6 - num);
}
}
if (currentCategory == 2) {
if (currentKey == 1) {
ethicScore += num;
}
if (currentKey == -1) {
ethicScore += (6 - num);
}
}
}
startGame();