-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbmi.js
52 lines (47 loc) · 1.99 KB
/
bmi.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
let button = document.getElementById('btn');
button.addEventListener('click', () => {
const height = parseInt(document.getElementById('height').value);
const weight = parseInt(document.getElementById('weight').value);
const result = document.getElementById('output');
let height_status=false, weight_status=false;
if(height === '' || isNaN(height) || (height <= 0)){
document.getElementById('height_error').innerHTML = 'Please provide a valid height';
}else{
document.getElementById('height_error').innerHTML = '';
height_status=true;
}
if(weight === '' || isNaN(weight) || (weight <= 0)){
document.getElementById('weight_error').innerHTML = 'Please provide a valid weight';
}else{
document.getElementById('weight_error').innerHTML = '';
weight_status=true;
}
if(height_status && weight_status){
const bmi = (weight / ((height*height)/10000)).toFixed(2);
if(bmi < 18.6){
result.innerHTML = 'Under weight : ' + bmi;
var mydiv = document.getElementById("output");
var aTag = document.createElement('a');
aTag.setAttribute('href',"Underweight.html");
aTag.innerText = "link text";
mydiv.appendChild(aTag);
}else if(bmi >= 18.6 && bmi < 24.9){
result.innerHTML = 'Normal : ' + bmi;
var mydiv = document.getElementById("output");
var aTag = document.createElement('a');
aTag.setAttribute('href',"Normal.html");
aTag.innerText = "link text";
mydiv.appendChild(aTag);
}else{
result.innerHTML = 'Over weight : ' + bmi;
var mydiv = document.getElementById("output");
var aTag = document.createElement('a');
aTag.setAttribute('href',"Overweight.html");
aTag.innerText = "link text";
mydiv.appendChild(aTag);
}
}else{
alert('Invalid input');
result.innerHTML = '';
}
});