-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.js
110 lines (96 loc) · 3.05 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
const question = document.getElementById("question");
const choices = Array.from(document.getElementsByClassName("choice-text"));
const questionCounterText= document.getElementById('questioncounter');
const scoreText = document.getElementById('score');
let currentQuestion = {};
let acceptingAnswers = false;
let score = 0;
let questioncounter = 0;
let availableQuestions = [];
let questions = [
{
question : "Urdu was declared national language of Pakistan in:",
choice1: "April 1950",
choice2: "April 1955",
choice3: "April 1954",
choice4: "April 1952",
answer : 3
},
{
question : "The first nuclear power plant in Pakistan was established at:",
choice1: "Karachi",
choice2: "Chashma",
choice3: "Mianwali",
choice4: "Rawalpindi",
answer : 1
},
{
question : "Which city is famous for chappal and Khussa?",
choice1: "Gawadar",
choice2: "Pishawar",
choice3: "Gujranwala",
choice4: "Sukkur",
answer : 2
},
{
question : " Which river flows through the Salt Range?",
choice1: "River Gomal",
choice2: "River Zobe",
choice3: "River Swat",
choice4: "River soan",
answer : 4
}
]
//Constants
const Correct_bonus = 10;
const Max_questions = 5;
startGame = () => {
questioncounter = 0;
score =0;
availableQuestions = [...questions];
getNewQuestion();
};
getNewQuestion = () => {
if(availableQuestions.length === 0 || questioncounter >= Max_questions){
localStorage.setItem('mostRecentScore', score);
// go to the end page
return window.location.assign("end.html")
}
questioncounter++;
questionCounterText.innerText = `${questioncounter}/${Max_questions}` ;
const questionIndex = Math.floor(Math.random() * availableQuestions.length);
currentQuestion = availableQuestions[questionIndex];
question.innerText = currentQuestion.question;
choices.forEach( choice => {
const number = choice.dataset['number'];
choice.innerText = currentQuestion['choice'+ number];
});
availableQuestions.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"];
//to give color
const classToApply = selectedAnswer == currentQuestion.answer ? 'correct'
: 'incorrect';
//=====Increase Score
if(classToApply == 'correct'){
incrementScore(Correct_bonus);
}
selectedChoice.parentElement.classList.add(classToApply);
//delay
setTimeout ( () => {
selectedChoice.parentElement.classList.remove(classToApply);
getNewQuestion();
}, 1000);
});
})
incrementScore = num => {
score += num;
scoreText.innerText= score;
}
startGame();