-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
109 lines (83 loc) · 2.37 KB
/
script.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
var questions = [];
var answers = [];
var currentQuestion = 0;
var score = 0;
function setup() {
var $ = function (id) {
return document.getElementById(id);
}
question = $("question");
answer = $("answer");
checkAnswerButton = $("checkAnswerButton");
correctButton = $("correctButton");
incorrectButton = $("incorrectButton");
newGameButton = $("newGameButton");
scoreText = $("score");
var randomQuestions = [];
var randomAnswers = [];
while (questions.length > 0) {
var pick = Math.floor(Math.random() * questions.length);
randomQuestions.push(questions[pick]);
randomAnswers.push(answers[pick]);
questions.splice(pick,1);
answers.splice(pick,1);
}
questions = randomQuestions;
answers = randomAnswers;
scoreText.innerHTML = "-";
showQuestion();
}
function showQuestion() {
question.innerHTML = questions[currentQuestion];
answer.innerHTML = " ";
hideAnswerButtons();
}
function showAnswer() {
answer.innerHTML = answers[currentQuestion];
showAnswerButtons();
}
function hideAnswerButtons() {
checkAnswerButton.style.display = "inline";
correctButton.style.display = "none";
incorrectButton.style.display = "none";
}
function showAnswerButtons() {
checkAnswerButton.style.display = "none";
correctButton.style.display = "inline";
incorrectButton.style.display = "inline";
}
function doCorrect() {
score = score + 1;
showNextQuestion();
}
function doIncorrect() {
showNextQuestion();
}
function showScore() {
if (score == currentQuestion) {
scoreText.innerHTML = "All correct: "+score;
}
else {
var percentage = Math.round(score/currentQuestion*100);
scoreText.innerHTML = score + " correct out of " + currentQuestion + " = " + percentage + "%";
}
}
function showNextQuestion() {
currentQuestion = currentQuestion+1;
showScore();
if (currentQuestion >= questions.length) {
alert("That's all the questions!\nYou got "+score+" questions right!");
checkAnswerButton.style.display = "none";
correctButton.style.display = "none";
incorrectButton.style.display = "none";
score = 0;
return;
}
showQuestion();
}
function newGame() {
currentQuestion = 0;
score = 0;
scoreText.innerHTML = "-";
showQuestion();
}