-
Notifications
You must be signed in to change notification settings - Fork 0
/
script.js
171 lines (151 loc) · 4.15 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
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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
const url =
"https://us-central1-ss-devops.cloudfunctions.net/rand?min=1&max=300";
const result = document.querySelector("#render-result");
const num = document.querySelector("#num");
const sub = document.querySelector("#sub");
const numberLedOne = document.querySelector("#first-number");
const numberLedTwo = document.querySelector("#second-number");
const numberLedTree = document.querySelector("#third-number");
const rematch = document.querySelector("#rematch-btn");
const rootCss = document.querySelector(":root");
let arrayNumbers = [];
let correctNumber;
let err = false;
let errorValue;
let objError;
function getData() {
fetch(url)
.then((res) => {
if (!res.ok) {
return res.text().then((text) => {
throw new Error((errorValue = text));
});
}
return res.json();
})
.then((data) => {
correctNumber = data.value;
})
.catch((error) => {
console.error(error);
getError();
});
}
function getAnswer(number) {
let selectedNumber = Number(number.value);
if (correctNumber === selectedNumber) {
return getCorrectAnswer();
}
if (correctNumber > selectedNumber) {
return (result.innerHTML = "É maior");
}
if (correctNumber < selectedNumber) {
return (result.innerHTML = "É menor");
}
}
function getCorrectAnswer() {
disableInputs();
result.innerHTML = "Você acertou!!!!!";
result.style.color = "#5dba38";
rootCss.style.setProperty("--smoothblack", "#5dba38");
rematch.style.display = "block";
}
function getError() {
arrayNumbers = [];
err = true;
objError = JSON.parse(errorValue);
result.innerHTML = "ERRO";
result.style.color = "#bf401f";
rematch.style.display = "block";
rootCss.style.setProperty("--smoothblack", "#bf401f");
splitToDigit(objError.StatusCode);
setLedNumbers();
disableInputs();
}
function disableInputs() {
num.disabled = true;
sub.disabled = true;
sub.style.cursor = "not-allowed";
sub.style.background = "#dddddd";
num.style.background = "#f5f5f5";
}
function enableInputs() {
num.disabled = false;
sub.disabled = false;
sub.style.cursor = "pointer";
sub.style.background = "linear-gradient(#da722d, #b7723a)";
num.style.background = "#ffffff";
}
function getNumber() {
if (verifyNum()) {
arrayNumbers = [];
splitToDigit(num.value);
setLedNumbers();
getAnswer(num);
num.value = "";
}
}
function verifyNum() {
if (num.value > 300) {
result.style.color = "#bf401f";
result.innerHTML = "Máximo 300!";
return false;
}
if (num.value < 1) {
result.style.color = "#bf401f";
result.innerHTML = "Mínimo 1!";
return false;
}
result.style.color = "#d5793d";
return true;
}
function splitToDigit(number) {
(number + "").split("").map((num) => {
arrayNumbers.push(num);
});
}
function setLedNumbers() {
switch (arrayNumbers.length) {
case 1:
numberLedOne.setAttribute("class", "num-" + arrayNumbers[0]);
numberLedTwo.style.display = "none";
numberLedTree.style.display = "none";
break;
case 2:
numberLedOne.setAttribute("class", "num-" + arrayNumbers[0]);
numberLedTwo.setAttribute("class", "num-" + arrayNumbers[1]);
numberLedTwo.style.display = "inline-flex";
numberLedTree.style.display = "none";
break;
case 3:
numberLedOne.setAttribute("class", "num-" + arrayNumbers[0]);
numberLedTwo.setAttribute("class", "num-" + arrayNumbers[1]);
numberLedTree.setAttribute("class", "num-" + arrayNumbers[2]);
numberLedTree.style.display = "inline-flex";
numberLedTwo.style.display = "inline-flex";
break;
}
}
function resetDisplayLed() {
numberLedOne.setAttribute("class", "num-0");
numberLedTwo.setAttribute("class", "num-0");
numberLedTree.setAttribute("class", "num-0");
}
function playAgain() {
initGame();
if (!err) {
result.innerHTML = "";
result.style.color = "#d5793d";
numberLedTwo.style.display = "none";
numberLedTree.style.display = "none";
rootCss.style.setProperty("--smoothblack", "#272b32");
enableInputs();
resetDisplayLed();
rematch.style.display = "none";
}
}
function initGame() {
err = false;
getData();
}
initGame();