-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
71 lines (45 loc) · 1.86 KB
/
script.js
File metadata and controls
71 lines (45 loc) · 1.86 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
const form = document.querySelector("form");
// click function
form.addEventListener('submit', function(e){
e.preventDefault();
const height = parseInt(document.querySelector("#height").value);
const weight = parseInt(document.querySelector("#weight").value);
const resultsText = document.querySelector('#results h4')
const results = document.querySelector('#results')
if((height === '' || height <= 0 || isNaN(height))){
resultsText.innerHTML = "Please give a valid Height and Weight";
}
else if((weight === '' || weight <= 0 || isNaN(weight))){
resultsText.innerHTML = "Please give a valid Height and Weight";
}
else{
const bmi = (weight / ((height * height) / 10000)).toFixed(2);
if(bmi < 18.6){
results.style.backgroundColor = "yellow";
results.style.color = "black";
resultsText.innerHTML = `You are under weight \n ${bmi}`;
}
else if((bmi >= 18.6) && (bmi <= 24.9) ){
results.style.backgroundColor = "green";
results.style.color = "white";
resultsText.innerHTML = `You are normal \n ${bmi}`;
}
else if((bmi > 24.9) && (bmi <= 40) ){
results.style.backgroundColor = "orange";
results.style.color = "white";
resultsText.innerHTML = `You are overweight \n ${bmi}`;
}
else{
results.innerHTML = `<h4>Sorry, you are experiencing obesity \n ${bmi}</h4>`;
results.style.color = "white";
results.style.backgroundColor = "red";
}
}
});
// reset code
function resetResults() {
results.innerHTML = "<h4>Try your BMI.</h4>";
results.style.backgroundColor = "";
}
height.addEventListener("input", resetResults);
weight.addEventListener("input", resetResults);