-
Notifications
You must be signed in to change notification settings - Fork 900
/
Copy pathapp.js
51 lines (40 loc) · 1.28 KB
/
app.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
const weightInput = document.getElementById('weight');
const heightInput = document.getElementById('height');
const resultElement = document.getElementById('result');
const bmiElement = document.getElementById('bmi');
const statusElement = document.getElementById('status');
const showResults = () => {
resultElement.style.display = 'block';
}
const hideResults = () => {
resultElement.style.display = 'none';
}
hideResults();
const calculateBMI = () => {
const weight = weightInput.value;
const height = heightInput.value;
if(weight && height){
const bmi = weight / (height ** 2);
if(isNaN(bmi)) {
hideResults();
return;
}
bmiElement.innerText = bmi.toFixed(2);
if (bmi < 18.5) {
statusElement.innerText = 'Underweight';
} else if (bmi < 25) {
statusElement.innerText = 'Normal';
} else if (bmi < 30) {
statusElement.innerText = 'Overweight';
} else {
statusElement.innerText = 'Obese';
}
showResults();
} else {
bmiElement.innerHTML = '';
statusElement.innerHTML = '';
hideResults();
}
}
weightInput.addEventListener('input', calculateBMI);
heightInput.addEventListener('input', calculateBMI);