-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathanswer.js
More file actions
83 lines (74 loc) · 2.9 KB
/
Copy pathanswer.js
File metadata and controls
83 lines (74 loc) · 2.9 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
let correctness = document.getElementById('correctness')
let numberOfRuns;
let numberOfSets = 3;
let correctAnswer;
fetch('https://memory-backend.herokuapp.com/score', {
method: 'GET'
}).then(response => {
return response.json();
}).then(JSONresponse => {
correctAnswer = JSONresponse.answer;
}).then(response => {
function handleClick() {
let userAnswer = document.getElementById('answer').value;
if (userAnswer === correctAnswer) {
correctness.innerHTML = 'Correct!';
correctness.style.color = 'green';
fetch('https://memory-backend.herokuapp.com/result/1', {
method: 'POST',
}).then(response => {
return response.json();
}).then(JSONresponse => {
console.log(JSONresponse);
}).catch(err => {
console.log('Nooooo!');
})
} else {
correctness.innerHTML = `Incorrect. The correct answer was ${correctAnswer}`;
correctness.style.color = 'red';
fetch('https://memory-backend.herokuapp.com/result/0', {
method: 'POST',
}).then(response => {
return response.json();
}).then(JSONresponse => {
console.log(JSONresponse);
}).catch(err => {
console.log('Nooooo!');
})
}
fetch('https://memory-backend.herokuapp.com/runs/1', {
method: 'POST',
}).then(response => {
return response.json();
}).then(JSONresponse => {
console.log(JSONresponse);
return JSONresponse;
}).catch(err => {
console.log('Nooooo!');
})
document.getElementById('submit').setAttribute('disabled', 'boolean');
}
function redirect() {
fetch('https://memory-backend.herokuapp.com/runs', {
method: 'GET'
}).then(response => {
return response.json();
}).then(JSONresponse => {
numberOfRuns = JSONresponse.runs;
numberOfSets = JSONresponse.numberOfSets;
console.log(JSONresponse);
}).then(finalResponse => {
if (document.getElementById('correctness').innerHTML === 'Correct!' || document.getElementById('correctness').innerHTML === `Incorrect. The correct answer was ${correctAnswer}`) {
if (numberOfRuns < numberOfSets) {
window.location.href = './functionality.html';
} else {
window.location.href = './score.html';
}
} else {
alert('Please submit an answer');
}
})
}
document.getElementById('submit').addEventListener('click', handleClick);
document.getElementById('next').addEventListener('click', redirect);
})