-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
109 lines (90 loc) · 3.43 KB
/
index.html
File metadata and controls
109 lines (90 loc) · 3.43 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
<!doctype html>
<html lang="en">
<head>
<title>TestMe</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1" />
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css" integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"
crossorigin="anonymous">
<style>
body {
background-color: darkgray;
}
.container div {
margin: 10px 0 10px 0;
}
#question {
background-color: rgb(233, 233, 233);
border-radius: 5px;
font-weight: bold;
}
#answer {
background-color: lightyellow;
display: none;
border-radius: 5px;
}
</style>
</head>
<body>
<div class="container">
<div class="col-sm-12" id="question"></div>
<div class="col-sm-12">
<button type="button" class="btn btn-info" id="displayAnswerBtn">Show Answer</button>
</div>
<div class="col-sm-12" id="answer"></div>
<div class="col-sm-12">
<button type="button" class="btn btn-primary" id="random">
Get random question/answer
</button>
</div>
</div>
<script type="text/javascript" src="https://code.jquery.com/jquery-3.3.1.slim.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js" integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
crossorigin="anonymous"></script>
<script>
let questionnaire = [];
let item = {};
const question = document.querySelector("#question");
const answer = document.querySelector("#answer");
const showAnswerBtn = document.querySelector("#displayAnswerBtn");
const randomBtn = document.querySelector("#random");
const questionnaireUrl = 'questionnaire.json';
const fetchFile = file => fetch(file)
.then(blob => blob.json()); // This one returns a Promise, it happens asynchronously, so you have to do what you want in the then()
const initData = (questionnaireUrl) => fetchFile(questionnaireUrl)
.then(data => {
questionnaire = data;
})
const displayQuestionnaireItem = () => {
getRandomQuestionnaireItem();
question.innerHTML = item.question;
answer.innerHTML = item.answer;
hideAnswer();
displayShowAnswerBtn();
}
const hideAnswer = () => {
answer.style.display = 'none';
}
const displayAnswer = () => {
answer.style.display = 'block';
hideShowAnswerBtn();
}
const hideShowAnswerBtn = () => {
showAnswerBtn.style.display = 'none';
}
const displayShowAnswerBtn = () => {
showAnswerBtn.style.display = 'block';
}
const getRandomQuestionnaireItem = () => {
item = questionnaire[Math.floor(Math.random() * questionnaire.length)];
}
const initDisplay = () => {
showAnswerBtn.addEventListener("click", displayAnswer);
randomBtn.addEventListener("click", displayQuestionnaireItem);
}
initData(questionnaireUrl)
.then(displayQuestionnaireItem)
.then(initDisplay);
</script>
</body>
</html>