-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
48 lines (41 loc) · 1.67 KB
/
script.js
File metadata and controls
48 lines (41 loc) · 1.67 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
// Use Fetch API to get the data from the JSON file
fetch('problems.json')
.then(response => {
// Parse the JSON data
return response.json();
})
.then(data => {
var solution = data.problems[0].solution;
// Use the parsed data (Now a JavaScript object)
var isShown = false;
var lastInt = null;
document.getElementById('newQuestion').addEventListener('click', function() {
var randomInt;
do {
randomInt = Math.floor(Math.random() * data.problems.length);
} while (randomInt === lastInt && data.problems.length > 1);
solution = data.problems[randomInt].solution;
lastInt = randomInt;
document.getElementById('question').innerHTML = data.problems[randomInt].name;
document.getElementById('description').innerHTML = data.problems[randomInt].description;
document.getElementById('number').innerText = data.problems[randomInt].QID;
document.getElementById('solution').innerHTML = null;
document.getElementById('showSolution').innerHTML = "Show Solution";
isShown = false;
});
document.getElementById('showSolution').addEventListener('click', function() {
if (!isShown) {
document.getElementById('solution').innerHTML = solution;
document.getElementById('showSolution').innerHTML = "Hide Solution";
isShown = true;
}
else {
document.getElementById('solution').innerHTML = null;
document.getElementById('showSolution').innerHTML = "Show Solution";
isShown = false;
}
});
})
.catch(error => {
console.error('Error fetching the JSON data:', error);
});