Following is a simple nodejs code wherein a "https" request is being made to the API URL("https://jsonmock.hackerrank.com/api/medical_records"), to get the total count of patients having Blood Pressure Diastole within the given range(lowerlimit, upperlimit). But I am unable to get the desired output (count) due to asynchronous behaviour. I have searched for all workaround methods like "async/await&Promise", "callbacks", etc. but I am unable to implement it in this particular example which might indicate I haven't yet understood those workaround methods. I would appreciate any help in explaining these methods in simple ways and thus, solving my problem. Thank You!!
function myfunc(lowerlimit, upperlimit) {
const https = require("https");
var url = "https://jsonmock.hackerrank.com/api/medical_records"
var totalPage;
var count = 0;
var medicalData;
var results;
var bloodPressureDiastole;
https.get(url, function(res){
res.on("data", function(data){
medicalData = JSON.parse(data);
totalPage = medicalData.total_pages;
});
});
for (let i = 1; i <= totalPage; i++){
url = url+"?page="+i.toString();
https.get(url, function(res){
res.on("data", function(data){
medicalData = JSON.parse(data);
results = medicalData.per_page;
for (let j = 0; j < results; j++){
bloodPressureDiastole = medicalData.data[j].vitals.bloodPressureDiastole;
if (bloodPressureDiastole >= lowerlimit && bloodPressureDiastole <= upperlimit) {
count++;
}
}
});
});
}
return count;
}
console.log(myfunc(80, 200));