-
Notifications
You must be signed in to change notification settings - Fork 29
/
Google Sheet Data as JSON for AJAX data
140 lines (133 loc) · 4 KB
/
Google Sheet Data as JSON for AJAX data
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
<!DOCTYPE html>
<html>
<head>
<title>Sheet Quiz</title>
<style>
*{
box-sizing:border-box;
}
.btn{
display:block;
margin:10px auto;
padding:10px;
}
.message{
width:80%;
border:1px solid #ddd;
padding:5px;
margin:auto;
}
.box{
display:inline-block;
border:1px solid #ddd;
padding:5px;
width:25%;
text-align:center;
text-transform:capitalize;
}
.box1:hover{
background-color:black;
color:white;
cursor:pointer;
}
</style>
</head>
<body>
<div class="output"></div>
<script src="app2.js"></script>
</body>
</html>
const url = 'https://script.google.com/macros/s/AKfycbxWcrVQhM0jX3bypudDtbquimzCceSWr3Ma4kwNKpHENA0EOXwSqvgQVpMXdVBDce7wQQ/exec';
const output = document.querySelector('.output');
const game = {question:0,total:0,data:[],score:0};
document.addEventListener('DOMContentLoaded',init);
function init(){
console.log('ready');
output.innerHTML = '';
const btn = document.createElement('button');
btn.disabled = true;
start(btn);
game.question = 0;
game.total = 0;
game.score = 0;
game.data =[];
fetch(url)
.then(res => res.json())
.then((data)=>{
console.log(data);
game.total = data.data.length;
game.data = data.data;
btn.disabled = false;
})
}
function start(btn){
const html = `Welcome to the quiz. Press the button below to start the QUIZ.`;
const div = maker('div',html,'message',output);
btn.textContent = 'Start Game';
btn.classList.add('btn');
div.append(btn);
btn.addEventListener('click',loadQuestion);
}
function loadQuestion(){
output.innerHTML = '';
if(game.question >= game.total){
const html = `<h1>Game Over</h1><div>You got ${game.score} out of ${game.total} correct.</div>`;
const div = maker('div',html,'message',output);
const btn3 = maker('button','Play Again','btn', div);
btn3.addEventListener('click',init);
}else{
const div = maker('div','','message',output);
const val = game.data[game.question];
//console.log(val.question);
const question = maker('div',`${val.question}?`,'question',div);
const optList = maker('div','','opts',div);
val.arr.forEach((opt)=>{
///console.log(opt);
const temp = maker('div',opt,'box',optList);
temp.classList.add('box1');
temp.myObj = {
opt:opt,
answer:val.answer
};
temp.addEventListener('click',checker);
})
}
//console.log('question');
}
function checker(e){
const val = e.target.myObj;
//console.log(val.opt);
//console.log(val.answer);
removeClicks();
e.target.style.color = 'white';
let html = '';
if(val.opt == val.answer){
game.score++;
e.target.style.backgroundColor = 'green';
html = `Correct!`;
}else{
e.target.style.backgroundColor = 'red';
html = `Wrong!`;
}
const parent = e.target.parentElement;
console.log(parent);
game.question++;
const rep = game.question == game.total ? 'End Game' : 'Next Question';
const feedback = maker('div',html,'message',parent);
const btn2 = maker('button',rep,'btn',parent);
btn2.addEventListener('click',loadQuestion);
}
function removeClicks(){
const boxes = document.querySelectorAll('.box');
boxes.forEach((ele)=>{
ele.removeEventListener('click',checker);
ele.style.color = '#ddd';
ele.classList.remove('box1');
})
}
function maker(eleType,html,cla,parent){
const ele = document.createElement(eleType);
ele.innerHTML = html;
ele.classList.add(cla);
return parent.appendChild(ele);
}