-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscripts.js
More file actions
184 lines (139 loc) · 3.85 KB
/
scripts.js
File metadata and controls
184 lines (139 loc) · 3.85 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
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
172
173
174
175
176
177
178
179
180
181
182
183
184
// IMC DATA
const data = [
{
min: 0,
max: 18.4,
classification: "Menor que 18,5",
info: "Magreza",
obesity: "0",
},
{
min: 18.5,
max: 24.9,
classification: "Entre 18,5 e 24,9",
info: "Normal",
obesity: "0",
},
{
min: 25,
max: 29.9,
classification: "Entre 25,0 e 29,9",
info: "Sobrepeso",
obesity: "I",
},
{
min: 30,
max: 39.9,
classification: "Entre 30,0 e 39,9",
info: "Obesidade",
obesity: "II",
},
{
min: 40,
max: 99,
classification: "Maior que 40,0",
info: "Obesidade grave",
obesity: "III",
},
];
// Seleção de elementos
const imcTable = document.querySelector("#imc-table");
const heightInput = document.querySelector("#height");
const weightInput = document.querySelector("#weight");
const calcBtn = document.querySelector("#calc-btn");
const clearBtn = document.querySelector("#clear-btn");
const calcContainer = document.querySelector("#calc-container")
const resultContainer = document.querySelector("#result-container")
const imcNumber = document.querySelector("#imc-number span");
const imcInfo = document.querySelector("#imc-info span");
const backBtn = document.querySelector("#back-btn");
// Funções
function createTable(data) {
data.forEach((item) => {
const div = document.createElement("div");
div.classList.add("table-data");
const classification = document.createElement("p");
classification.innerText = item.classification;
const info = document.createElement("p");
info.innerText = item.info;
const obesity = document.createElement("p");
obesity.innerText = item.obesity;
div.appendChild(classification)
div.appendChild(info)
div.appendChild(obesity)
imcTable.appendChild(div);
})
}
function cleanInputs() {
heightInput.value="";
weightInput.value="";
imcNumber.classList ="";
imcInfo.classList = "";
}
function validDigits(text) {
return text.replace(/[^0-9,]/g, "")
}
function calcImc(weight, height) {
const imc = (weight / ( height * height)).toFixed(1);
return imc;
}
function showOrHideResults() {
calcContainer.classList.toggle("hide")
resultContainer.classList.toggle("hide")
}
// Inicializações
createTable(data);
//Eventos
[heightInput,weightInput].forEach((el) => {
el.addEventListener("input", (e) => {
const updateValue = validDigits(e.target.value);
e.target.value = updateValue;
})
});
calcBtn.addEventListener("click", (e) => {
e.preventDefault();
const weight = +weightInput.value.replace(",", ".")
const height = +heightInput.value.replace(",", ".")
if(!weight || !height) return;
const imc = calcImc(weight, height)
let info
data.forEach((item) => {
if (imc >= item.min && imc <= item.max){
info = item.info;
}
})
if (!info) return;
imcNumber.innerText = imc;
imcInfo.innerText = info;
switch(info){
case "Magreza":
imcNumber.classList.add("low");
imcInfo.classList.add("low");
break;
case "Normal":
imcNumber.classList.add("good");
imcInfo.classList.add("good");
break;
case "Sobrepeso":
imcNumber.classList.add("low");
imcInfo.classList.add("low");
break;
case "Obesidade":
imcNumber.classList.add("medium");
imcInfo.classList.add("medium");
break;
case "Obesidade grave":
imcNumber.classList.add("high");
imcInfo.classList.add("high");
break;
}
showOrHideResults();
})
clearBtn.addEventListener("click", (e) =>{
e.preventDefault()
cleanInputs();
});
backBtn.addEventListener("click", () => {
cleanInputs();
showOrHideResults();
})