-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
145 lines (118 loc) · 3.63 KB
/
game.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
document.getElementById('timer').innerHTML =
001 + ":" + 00;
startTimer();
function startTimer() {
var presentTime = document.getElementById('timer').innerHTML;
var timeArray = presentTime.split(/[:]+/);
var m = timeArray[0];
var s = checkSecond((timeArray[1] - 1));
if (s == 59) { m = m - 1 }
if (m < 0) {
alert('Time is Up!');
return;
}
document.getElementById('timer').innerHTML =
m + ":" + s;
console.log(m)
setTimeout(startTimer, 1000);
}
function checkSecond(sec) {
if (sec < 10 && sec >= 0) { sec = "0" + sec };
if (sec < 0) { sec = "59" };
return sec;
};
const question = document.getElementById("question");
const choices = Array.from(document.getElementsByClassName("choice-text"));
const scoreText = document.getElementById('score');
let currentQuestion = {};
let acceptingAnswers = true;
let score = 0;
let questionCounter = 0;
let availableQuestions = [];
//List of questions
var questions = [
{
question: "Inside the HTML which tag do we use for Javascript?",
choice1: "<javascript>",
choice2: "<style.js>",
choice3: "<script>",
answer: 3
},
{
question: "How do you write 'Hello!' in an alert box?",
choice1: "msgBox('Hello!');",
choice2: "alert('Hello!');",
choice3: "alertBox('Hello!');",
answer: 2
},
{
question: "The condition of an if/else statement is enclosed within ____.",
choice1: "quotes",
choice2: "curly brackets",
choice3: "parantheses",
answer: 3
},
{
question: "Commonly used data types DO NOT include:",
choice1: "alerts",
choice2: "strings",
choice3: "booleans",
answer: 1
},
{
question: "What is the correct syntax referring to the external script called 'xxx.js'?",
choice1: "<script href='xxx.js'>",
choice2: "<script src='xxx.js'>",
choice3: "<script name='xxx.js'>",
answer: 2
}
];
//CONSTANTS
const CORRECT_BONUS = 10;
const MAX_QUESTIONS = 5;
startGame = () => {
questionCounter = 0;
score = 0;
availableQuesions = [...questions];
getNewQuestion();
};
getNewQuestion = () => {
if (availableQuesions.length === 0 || questionCounter >= MAX_QUESTIONS) {
localStorage.setItem("mostRecentScore", score);
//go to the end page
return window.location.assign('end.html');
}
questionCounter++;
const questionIndex = Math.floor(Math.random() * availableQuesions.length);
currentQuestion = availableQuesions[questionIndex];
question.innerText = currentQuestion.question;
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'];
const classToApply =
selectedAnswer == currentQuestion.answer ? "correct" : "incorrect";
if (classToApply === "correct") {
incrementScore(CORRECT_BONUS);
}
selectedChoice.parentElement.classList.add(classToApply);
setTimeout(() => {
selectedChoice.parentElement.classList.remove(classToApply);
getNewQuestion();
}, 1000);
});
});
incrementScore = num => {
score += num;
scoreText.innerText = score;
};
startGame();