-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
176 lines (130 loc) · 5.55 KB
/
Copy pathapp.js
File metadata and controls
176 lines (130 loc) · 5.55 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/*
SoC - wk2 - HACKATHON - API APPS
Quiz Generator
Layout Ideas
Section 0 - Welcome and instructions
Section 1 - Player Profile
Section 2 - Quiz Generator
Section 3 - Game Area
Section 4 - Score Board
*** Task 0 - Welcome and instructions
- Display a welcome message ✅
- Display instructions on how to play the game ✅
- Have instructions on a toggle button that shows or hides the instructions ✅
*** Task 1 - Player Profile
- Prompt the user for their name ✅
- Store the name in a variable ✅
- Display the name in the player profile section (h1) ✅
*** Task 2 - Quiz Generator
Some limitations to be placed on available options
- choose from 5 categories - gen knowledge (9), music (12), film (11), science (17), celebs (26) ✅
- choose from 3 difficulty levels (only if time allows) ✅
- button to generate quiz ✅
display dropdown menu with options for categories ✅
create a function that requests the api using string interpolation based on user selections ✅
retrieve the data from the api and store in a variable ✅
*** Task 3 - Game Area
- Ask each question ✅ (one at a time and wait for the user to answer)
- When an answer is received, check if it is correct (and store the result) ✅
- ((Ask next question))
*** Task 4 - Scoring
- When all questions have been asked, display the results as the number of correct answers out of the total number of questions
*** task 5 - Ask player if they want to play again
- If yes, reset the game
- If no, display a goodbye message
*/
// Section 0 - welcome and instructions
// Display a welcome message
function welcomeAlert() {
let welcome = alert('Welcome to the Quiz Generator');
return welcome;
}
welcomeAlert();
// Instructions button
let instructionsButton = document.getElementById('instructions');
// Display instructions on how to play the game
function instructions() {
let instructions = window.alert('Instructions: \n 1. Choose a category \n 2. Choose a difficulty level \n 3. Click the Generate Quiz button \n 5. Test your knowledge! \n 6. Refresh the page to play again');
//console.log(instructions)
return instructions;
}
// Task 1 - Player Profile
let playerName = prompt('Please enter your name');
document.getElementById('playerName').textContent = playerName;
// Task 2 - Quiz Generator
// create a function to save the players quiz choices and stores them as variables
let category = document.getElementById('category-select').value;
let difficulty = document.getElementById('difficulty-select').value;
// Create an empty array for questions
let quizQuestions = [];
// Takes in choices and returns the information from the API
function getQuizOptions() {
const category = document.querySelector("#category-select").value;
const difficulty = document.querySelector("#difficulty-select").value;
const apiUrl = `https://opentdb.com/api.php?amount=10&category=${category}&difficulty=${difficulty}&type=boolean`;
fetch(apiUrl)
.then(response => response.json())
.then(data => {
const quizQuestions = data.results.map(result => {
return {
question: result.question,
correctAnswer: result.correct_answer
};
});
//console.log("Quiz questions:", quizQuestions[0].question);
// call the function
displayQuizQuestions(quizQuestions);
})
.catch(error => {
console.error("Error fetching quiz questions:", error);
});
// console.log(quizQuestions);
// console.log("Selected Category:", category);
// console.log("Selected Difficulty:", difficulty);
}
// Displays questions
function displayQuizQuestions(quizQuestions) {
// Get a reference to the quiz container element
const quizContainer = document.querySelector("#quiz-container");
quizContainer.innerHTML = "";
// Loop through each quiz question and create a new element to display it
quizQuestions.forEach((question, index) => {
const questionElement = document.createElement("div");
questionElement.textContent = `${index + 1}. ${question.question}`;
quizContainer.appendChild(questionElement);
const trueButton = document.createElement("button");
trueButton.textContent = "True";
trueButton.style.marginLeft = "10px";
questionElement.appendChild(trueButton);
const falseButton = document.createElement("button");
falseButton.textContent = "False";
falseButton.style.marginLeft = "10px";
questionElement.appendChild(falseButton);
// Add event listeners to the buttons to check answers
trueButton.addEventListener("click", () => {
checkAnswer("True", question.correctAnswer);
});
falseButton.addEventListener("click", () => {
checkAnswer("False", question.correctAnswer);
});
// Function to check the user's answer against the correct answer
function checkAnswer(userAnswer, correctAnswer) {
if (userAnswer === correctAnswer) {
// User's answer is correct
const correctAnswerElement = document.createElement("div");
correctAnswerElement.textContent = "Correct!";
correctAnswerElement.style.fontSize = "18px";
questionElement.appendChild(correctAnswerElement);
// console.log("Correct!");
} else {
// User's answer is incorrect
const incorrectAnswerElement = document.createElement("div");
incorrectAnswerElement.textContent = "Incorrect!";
incorrectAnswerElement.style.fontSize = "18px";
questionElement.appendChild(incorrectAnswerElement);
// console.log("Incorrect!");
}
}
});
}
// const generateQuizButton = document.getElementById('generate-quiz');