-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
153 lines (118 loc) · 4.62 KB
/
app.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
var gameState = {
correctAnswers: 0,
questionCount: 0,
currentChoice: undefined
};
var questions = {
Q1: {
question: "Which woman was the lead software engineer on the Apollo?",
answer: "Margaret Hamilton",
choices: ["Margaret Hamilton", "Grace Hopper", "Annie Easley", "Mary Jackson"]
},
Q2: {
question: "Which woman is credited for coining the term 'computer bug' ?",
answer: "Grace Hopper",
choices: ["Ada Lovelace", "Grace Hopper", "Anita Borg", "Lynn Conway"]
},
Q3: {
question: "Which woman is said to be the world's first computer programmer?",
answer: "Ada Lovelace",
choices: ["Grace Hopper", "Radia Perlman", "Ada Lovelace", "Anita Borg"]
},
Q4: {
question: "Which woman was NOT portrayed in the movie Hidden Figures?",
answer: "Grace Hopper",
choices: ["Dorothy Vaughan", "Grace Hopper", "Mary Jackson", "Katherine Johnson"]
},
Q5: {
question: "Which woman computer scientist, electrical engineer, inventor is also a transgender activist?",
answer: "Lynn Conway",
choices: ["Lynn Conway", "Mary Jackson", "Anita Borg", "Radia Perman"]
},
};
function questionCount(gameState) {
gameState.questionCount += 1;
}
function initiateGame(questions, gameState) {
$(".question").html(questions.Q1.question);
$(".a").html(questions.Q1.choices[0]);
$(".b").html(questions.Q1.choices[1]);
$(".c").html(questions.Q1.choices[2]);
$(".d").html(questions.Q1.choices[3]);
// $(".answer-tally").html(gameState.questionCount);
questionCount(gameState);
}
initiateGame(questions, gameState);
function changeQuestion(questions, gameState) {
questionCount(gameState);
var currentSelector = 'Q' + gameState.questionCount;
var currentQuestion = questions[currentSelector];
if (gameState.questionCount <= 5) {
$(".question").html(currentQuestion.question);
$(".a").html(currentQuestion.choices[0]);
$(".b").html(currentQuestion.choices[1]);
$(".c").html(currentQuestion.choices[2]);
$(".d").html(currentQuestion.choices[3]);
$(".question-number").html(gameState.questionCount);
$(".answer-tally").html(gameState.correctAnswers);
} else {
showFinalScore();
}
}
function showFinalScore() {
$(".question").html("Your final score is <span class='correct'> " + gameState.correctAnswers + "</span> out of 5");
$(".buttons").html("<a href='game.html'><button class='btn btn-default btn-lg center-block gamelink'> Play Again?</button></a>");
$(".list-group").hide();
$(".correct-or-incorrect").hide();
$(".answer-tally").hide();
$(".question-number").hide();
$(".questions").hide();
$(".answers").hide();
}
function submitAnswer(event) {
// Determine if answer is correct
var currentSelector = 'Q' + gameState.questionCount;
var currentQuestion = questions[currentSelector];
console.log(gameState.currentChoice);
console.log(currentQuestion.answer);
if (gameState.currentChoice === currentQuestion.answer) {
$(".correct-or-incorrect").html("Correct!").addClass("correct").removeClass("incorrect").removeClass('hidden');
updateAnswerTalley(gameState);
changeQuestion(questions, gameState);
} else {
incorrectAnswer(currentQuestion);
}
// find all elements with .active class\
$('.active').removeClass('active');
// remove .active from those elements
}
function incorrectAnswer(currentQuestion){
$(".correct-or-incorrect").html("Incorrect! The correct answer was " + currentQuestion.answer).addClass("incorrect").removeClass("correct").removeClass('hidden');
// 0. Hide original answer selections
$('.list-group').addClass('hidden');
// 2. Hide original submit button
$('#submit-next').addClass('hidden');
// 3. Display incorrect-submit button
$('#submit-incorrect').removeClass('hidden');
}
function updateAnswerTalley(gameState) {
gameState.correctAnswers += 1;
}
function test() {
console.log("success!");
}
$(function() {
$('.list-group-item').on('click', function(event) {
$('.list-group-item').removeClass('active');
$(event.target).addClass('active');
gameState.currentChoice = event.target.innerHTML;
});
$('#submit-next').on('click', submitAnswer);
$('#submit-incorrect').on('click', function(event){
changeQuestion(questions, gameState);
$('#submit-next').removeClass('hidden');
$('.list-group').removeClass('hidden');
$('#submit-incorrect').addClass('hidden');
$('.correct-or-incorrect').addClass('hidden');
});
});