-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.js
98 lines (76 loc) · 2.18 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
const question = document.getElementById('question');
const choice = Array.from(document.getElementsByClassName('choice-text'))
let currentQuestion = 0;
let acceptingAnswers = false;
let score = 0;
let availableQuestions = [];
let questionCounter = 0;
let questions = [
{
question: "Who is the first president of Nigeria?",
choice1: "Alh Sir Abubakar Tafawa Balewa.",
choice2: "General Yakubu Gowon",
choice3: "Alhaji Shehu Shagari",
choice4: "Dr Nnamdi Azikwe",
answer: 3
},
{
question: "Who is the India's largest trading partner in Africa?",
choice1: "Mauritus",
choice2: "Morroco",
choice3: "South Africa",
choice4: "Nigeria",
answer: 4
},
{
question: "Nigeria is a member of the following organizations except:",
choice1: 'African Union',
choice2: 'G-20',
choice3: 'G-19',
choice4: 'Interpol',
answer: 2
},
{
question: "Adamawa state was named after the warrior ____ that conquered the region in the beginning of the 19th century.",
choice1: 'Modibbo Adama Bin Ardo Hassan',
choice2: 'Modibbo Adama Bin Usman',
choice3: 'Modibbo Adama Ardo Hassan',
choice4: 'Modibbo Adama',
answer: 1
}
];
//CONSTANTS
const questionBonus = 10;
const maxQuestions = 3;
startGame = () => {
questionCounter = 0;
score = 0;
availableQuestions = [...questions];
getNewQuestions();
}
getNewQuestions = () => {
if(availableQuestions.length === 0 || questionCounter >= maxQuestions){
window.location.assign('end.html');
}
questionCounter++;
const questionIndex = Math.floor(Math.random() * availableQuestions.length);
currentQuestion = availableQuestions[questionIndex];
question.innerText = currentQuestion.question;
choice.forEach(choice => {
const number = choice.dataset['number'];
choice.innerText = currentQuestion['choice' + number];
})
availableQuestions.splice(questionIndex, 1);
acceptingAnswers = true;
}
choice.forEach(choice => {
choice.addEventListener('click', e =>{
if(!acceptingAnswers) return;
acceptingAnswers = false;
const selectedChoice = e.target;
const selectedAnswer = selectedChoice.dataset['number'];
getNewQuestions();
}
)
})
startGame();