This repository has been archived by the owner on Dec 22, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
73 lines (58 loc) · 2.18 KB
/
script.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
const apiUrl = 'https://opentdb.com/api.php?amount=3&category=23&difficulty=medium';
console.log('1 Script started');
window.addEventListener('load', () => {
console.log('2 Window load event');
// skicka request till api-servern
// ta hand om information vi får tillbaka
// visa datan på webbsidan
// förbättra design och CSS
let bert = document.querySelector('#bert');
bert.addEventListener('click', async e => {
console.log('3 Clicked on Bert');
// Alternativt sätt att använda fetch:
// fetch(apiUrl).then(response => { })
const response = await fetch(apiUrl);
// the server returns a Promise (with a Response object)
console.log('4 Got response from server', response);
const data = await response.json();
// The response contains a string in JSON format. To convert JSON we would normally type: let object = JSON.parse(string)
// BUT response.json() is a Promise that does the parsing for us
console.log('5 JSON data is: ', data);
// data.results[]
let allQuestions = data.results;
let questionContainer = document.querySelector('#questionContainer');
allQuestions.forEach(q => {
let element = createQuestionDOM(q);
questionContainer.appendChild(element);
})
})
});
function createQuestionDOM(question) {
// { question, correct_answer, incorrect_answers }
let questionElement = document.createElement('div');
questionElement.className = 'question';
// Alternativ: questionElement.classList
let questionHeading = document.createElement('h2');
questionHeading.innerText = question.question;
questionElement.appendChild(questionHeading);
// ... creates a copy of the list (spread operator)
let options = [...question.incorrect_answers, question.correct_answer];
// TODO: shuffle the array!!
// Alternative: question.incorrect_answers.push(..)
options.forEach(o => {
let element = document.createElement('div');
element.className = 'option';
element.innerText = o;
element.addEventListener('click', e => {
console.log('You chose: ' + o);
if( o === question.correct_answer ) {
console.log('CORRECT!!');
} else {
console.log('Incorrect. Try again!');
}
})
questionElement.appendChild(element);
});
return questionElement;
}
//