forked from Sulagna-Dutta-Roy/GGExtensions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
popup.js
72 lines (62 loc) · 2.43 KB
/
popup.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
document.addEventListener('DOMContentLoaded', () => {
var age = document.getElementById("age");
var height = document.getElementById("height");
var weight = document.getElementById("weight");
var male = document.getElementById("m");
var female = document.getElementById("f");
var resultArea = document.querySelector(".comment");
var modal = document.getElementById("myModal");
var modalText = document.getElementById("modalText");
var span = document.getElementsByClassName("close")[0];
function calculate() {
if (age.value === '' || height.value === '' || weight.value === '' || (!male.checked && !female.checked)) {
showModal('ALL fields are required!');
} else if (!isPositiveNumber(height.value) || !isPositiveNumber(weight.value)) {
showModal('Please enter valid positive values for height and weight!');
} else {
countBmi();
}
}
function isPositiveNumber(value) {
return /^\d*\.?\d+$/.test(value) && parseFloat(value) > 0;
}
function countBmi() {
var p = [age.value, height.value, weight.value];
if (male.checked) {
p.push("male");
} else if (female.checked) {
p.push("female");
}
var bmi = Number(p[2]) / (Number(p[1]) / 100 * Number(p[1]) / 100);
var result = '';
if (bmi < 18.5) {
result = 'Underweight';
} else if (18.5 <= bmi && bmi < 25) {
result = 'Healthy';
} else if (25 <= bmi && bmi < 30) {
result = 'Overweight';
} else if (30 <= bmi && bmi < 35) {
result = 'Obese (Class 1)';
} else if (35 <= bmi && bmi < 40) {
result = 'Obese (Class 2)';
} else if (bmi >= 40) {
result = 'Obese (Class 3)';
}
resultArea.style.display = "block";
document.querySelector(".comment").innerHTML = `You are <span id="comment">${result}</span>`;
document.querySelector("#result").innerHTML = bmi.toFixed(2);
}
function showModal(message) {
modal.style.display = "block";
modalText.innerHTML = message;
}
span.onclick = function() {
modal.style.display = "none";
}
window.onclick = function(event) {
if (event.target == modal) {
modal.style.display = "none";
}
}
document.getElementById("submit").addEventListener("click", calculate);
});