-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
82 lines (69 loc) · 4.68 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
const resetButton = document.querySelector(".reset-button");
const calculateButton = document.querySelector(".calculate-button");
const inputWeight = document.querySelector('.weight');
const inputHeight = document.querySelector('.height');
const result = document.querySelector(".result");
const calculateIMC = () => {
const weight = parseFloat(inputWeight.value);
const height = parseFloat(inputHeight.value);
const imc = (weight / (height * height)).toFixed(2);
if (!isNaN(imc)) {
if (imc > 0.00 && imc <= 18.49) {
result.innerHTML = `<h2 style="text-align: center; margin-top: 35px; color: #FF6666; font-size: 15px"><strong>Seu IMC é ${imc}</strong></h2><h1 style="color:#FF6666; text-align: center;">🤕 Abaixo do peso!</h1> <p style="color:#FF6666; text-align: center;">Cuidado! Seu IMC está abaixo do recomendado. É importante equilibrar sua alimentação e consultar um profissional de saúde para avaliar sua situação.</p>`
} else if (imc <= 24.99) {
result.innerHTML = `<h2 style="text-align: center; margin-top: 35px; color: #32CD32; font-size: 15px"><strong>Seu IMC é ${imc}</strong></h2><h1 style="color:#32CD32; text-align: center;">😉 Peso normal!</h1> <p style="color:#32CD32; text-align: center;">Parabéns! Seu IMC está dentro do intervalo considerado saudável. Mantenha uma alimentação equilibrada e pratique atividades físicas regularmente para manter sua saúde em dia.</p>`
} else if (imc <= 29.99) {
result.innerHTML = `<h2 style="text-align: center; margin-top: 35px; color: #e6af38; font-size: 15px"><strong>Seu IMC é ${imc}</strong></h2><h1 style="color:#e6af38; text-align: center;">😯 Sobrepeso!</h1> <p style="color:#e6af38; text-align: center;">Atenção! Seu IMC está acima do recomendado. É importante equilibrar sua alimentação, praticar atividades físicas regularmente e, se necessário, consultar um profissional de saúde para avaliar sua situação.</p>`
} else if (imc <= 39.99) {
result.innerHTML = `<h2 style="text-align: center; margin-top: 35px; color: #fa8116; font-size: 15px"><strong>Seu IMC é ${imc}</strong></h2><h1 style="color:#fa8116; text-align: center;">😬 Obesidade!</h1> <p style="color:#fa8116; text-align: center;">Urgente! Seu IMC indica obesidade. É importante procurar imediatamente um profissional de saúde para avaliar sua situação e prescrever as medidas necessárias para cuidar de sua saúde.</p>`
} else {
result.innerHTML = `<h2 style="text-align: center; margin-top: 35px; color: #c91e12; font-size: 15px"><strong>Seu IMC é ${imc}</strong></h2><h1 style="color:#c91e12; text-align: center;">🚨 Obesidade grave!</h1> <p style="color:#c91e12; text-align: center;">Emergência! Seu IMC indica obesidade grave. É de extrema importância procurar imediatamente um profissional de saúde para avaliar sua situação e prescrever as medidas necessárias para cuidar de sua saúde antes que seja tarde demais.</p>`
}
} else {
alert("Prencha todos os dados!")
}
}
const formatHeight = (event) => {
const valueEvent = event.target.value;
const numericValue = valueEvent.replace(/\D/g, '');
if (numericValue.length < 3) {
event.target.value = numericValue;
return;
}
const firstPart = numericValue.slice(0, 1);
const secondPart = numericValue.slice(1);
const formattedHeight = `${firstPart}.${secondPart}`;
event.target.value = formattedHeight;
}
const formatWeight = (event) => {
const valueEvent = event.target.value;
const numericValue = valueEvent.replace(/\D/g, ''); // remove todos os caracteres não numéricos
if (numericValue.length < 3) {
event.target.value = numericValue;
return;
}
let formattedValue;
if (numericValue.length === 2) {
const firstPart = numericValue.slice(0, 2);
formattedValue = `${firstPart}`;
} else if (numericValue.length === 5) {
const firstPart = numericValue.slice(0, 3);
const secondPart = numericValue.slice(3);
formattedValue = `${firstPart}.${secondPart}`;
} else {
const firstPart = numericValue.slice(0, 2);
const secondPart = numericValue.slice(2, 5);
const thirdPart = numericValue.slice(5);
formattedValue = `${firstPart}.${secondPart}${thirdPart}`;
}
event.target.value = formattedValue;
}
const resetData = () => {
inputWeight.value = "";
inputHeight.value = "";
result.innerHTML = "";
}
calculateButton.addEventListener('click', calculateIMC);
resetButton.addEventListener('click', resetData);
inputHeight.addEventListener('input', formatHeight);
inputWeight.addEventListener('input', formatWeight);