-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
24 lines (21 loc) · 943 Bytes
/
script.js
File metadata and controls
24 lines (21 loc) · 943 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
const form = document.querySelector('form');
form.addEventListener('submit', function (e) {
e.preventDefault();
const height = parseInt(document.querySelector('#height').value);
const weight = parseInt(document.querySelector('#weight').value);
const results = document.querySelector('#results');
if (height === '' || height < 0 || isNaN(height)) {
results.innerHTML = `please enter a valid height ${height}`;
} else if (weight === '' || weight < 0 || isNaN(weight)) {
results.innerHTML = `please enter a valid weight ${weight}`;
} else {
const bmi = (weight / ((height * height) / 10000)).toFixed(2);
if (bmi < 18.6) {
results.innerHTML = `<span>BMI = ${bmi}</span> <p>Underweight</p>`;
} else if (bmi == 18.6 || bmi < 24.9) {
results.innerHTML = `<span>BMI = ${bmi}</span> <p>Normal Range</p`;
} else {
results.innerHTML = `<span>BMI = ${bmi}</span> <p>Overweight</p`;
}
}
});