-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
148 lines (137 loc) · 4.8 KB
/
script.js
File metadata and controls
148 lines (137 loc) · 4.8 KB
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
// HTML elements
var quizBody = document.getElementById("quiz");
var resutsElement= document.getElementById("result");
var finalScoreEl = document.getElementById("finalScore");
var gameoverDiv = document.getElementById("gameover");
var questionsEl = document.getElementById("questions");
var quizTimer = document.getElementById("timer");
var startQuizButton = document.getElementById("startbtn");
var startQuizDiv = document.getElementById("startpage");
var endGameBtns = document.getElementById("endGameBtns");
var submitScoreBtn = document.getElementById("submitScore");
var buttonA = document.getElementById("a");
var buttonB = document.getElementById("b");
var buttonC = document.getElementById("c");
var buttonD = document.getElementById("d");
// Questions object
var quizQuestions = [{
question: "Where is the country of Brazil located?",
choiceA: "Indiana",
choiceB: "Asian Continent",
choiceC: "South America",
choiceD: "A block from my house",
correctAnswer: "c"},
{
question: "What language is spoken in Brazil?",
choiceA: "Portuguese",
choiceB: "Spanish",
choiceC: "A loud language",
choiceD: "No language assigned",
correctAnswer: "a"},
{
question: "Where does the name BRAZIL come from?",
choiceA: "idk, I did not name it",
choiceB: "A tree named brazilwood",
choiceC: "Brazilans",
choiceD: "Cabral cousin named it",
correctAnswer: "b"},
{
question: "What have Brazil produced for over 150 years, and is very well known for?",
choiceA: "Corn",
choiceB: "Soy Beans",
choiceC: "Cheetos",
choiceD: "Coffee",
correctAnswer: "d"},
{
question: "Where is the deadlist place in Brazil?",
choiceA: "Snake Island in Queimada Grande",
choiceB: "The whole country",
choiceC: "Rio de Janeiro",
choiceD: "I don't even want to know",
correctAnswer: "a"},
{
question: "What most cars in Brazil use as fuel?",
choiceA: "Gasoline",
choiceB: "Grass",
choiceC: "Ethanol",
choiceD: "Beans",
correctAnswer: "c"},
{
question: "What does the word BOLO mean in Portuguese?",
choiceA: "Head",
choiceB: "Cake",
choiceC: "Beautiful",
choiceD: "Smart",
correctAnswer: "b"},
];
// Global variables
var finalQuestionIndex = quizQuestions.length;
var currentQuestionIndex = 0;
var timeLeft = 76;
var timerInterval;
var score = 0;
var correct;
// This function generate the questions and answers.
function generateQuizQuestion(){
gameoverDiv.style.display = "none";
if (currentQuestionIndex === finalQuestionIndex){
return showScore();
}
var currentQuestion = quizQuestions[currentQuestionIndex];
questionsEl.innerHTML = "<p>" + currentQuestion.question + "</p>";
buttonA.innerHTML = currentQuestion.choiceA;
buttonB.innerHTML = currentQuestion.choiceB;
buttonC.innerHTML = currentQuestion.choiceC;
buttonD.innerHTML = currentQuestion.choiceD;
};
// Start Quiz function starts the TimeRanges, hides the start button, and displays the first quiz question.
function startQuiz(){
gameoverDiv.style.display = "none";
startQuizDiv.style.display = "none";
generateQuizQuestion();
//Timer
timerInterval = setInterval(function() {
timeLeft--;
quizTimer.textContent = "Time left: " + timeLeft;
if(timeLeft === 0) {
clearInterval(timerInterval);
showScore();
}
}, 1000);
quizBody.style.display = "block";
}
// Function for displaying your score
function showScore(){
quizBody.style.display = "none"
gameoverDiv.style.display = "flex";
clearInterval(timerInterval);
finalScoreEl.innerHTML = "You got " + score + " out of " + quizQuestions.length + " correct!";
}
// This function is to replay quiz
function replayQuiz(){
gameoverDiv.style.display = "none";
startQuizDiv.style.display = "flex";
timeLeft = 76;
score = 0;
currentQuestionIndex = 0;
}
// This function checks the response to each answer
function checkAnswer(answer){
correct = quizQuestions[currentQuestionIndex].correctAnswer;
if (answer === correct && currentQuestionIndex !== finalQuestionIndex){
score++;
alert("That Is Correct!");
currentQuestionIndex++;
generateQuizQuestion();
//display in the results div that the answer is correct.
}else if (answer !== correct && currentQuestionIndex !== finalQuestionIndex){
alert("That Is Incorrect.")
currentQuestionIndex++;
generateQuizQuestion();
//display in the results div that the answer is wrong.
}else{
showScore();
}
}
// This button starts the quiz!
startQuizButton.addEventListener("click",startQuiz);