-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearchCategory.js
54 lines (42 loc) · 1.55 KB
/
searchCategory.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
const btn = document.querySelector('button');
var weight = document.getElementById('weight'); //Receive weight value
var height = document.getElementById('height'); //Receive height value
var result = document.getElementById('result') //Receive result value
var resultcategory = document.getElementById('resultBmi')
var error =''; // variable that receives error messages
var categoryBmi ='';// variable that receives category
btn.addEventListener('click', () => {
console.log("searching categories...");
if (weight.value==''){
error ="type a value for weight";
alert(error)
}
else if(height.value =='')
{
error ="type a value for height";
alert(error)
}else{
var weight_right = parseFloat(weight.value)
var height_right = parseFloat(height.value)
var bmi = calculateBmi(weight_right,height_right)
var xhr = new XMLHttpRequest();
xhr.open("GET", "listing.js");
xhr.addEventListener("load", function() {
var answer = xhr.responseText;
//console.log(answer)
var categories = JSON.parse(answer);
categories.forEach(function(category) {
if ((bmi > category.bmi_minimum) && (bmi < category.bmi_maximum ))
{
categoryBmi = category.category
}
resultcategory.innerHTML = categoryBmi
});
});
xhr.send();
}
result.value = bmi.toFixed(2)
});
function calculateBmi(weight , height){
return (weight / (height * height))
}