I am solving a question on hackerrank to print country name for the corresponding country code. I am getting the correct result in my console log statement but getting undefined in my output. The function is given below and i have commented the line on which I am getting undefined.
async function getCountryName(code) {
// write your code here
// API endpoint: https://jsonmock.hackerrank.com/api/countries?page=<PAGE_NUMBER>
for(var i =1 ;i<26;i++)
{
const url = "https://jsonmock.hackerrank.com/api/countries?page=" + i;
https.get(url, resp => {
let data = '';
resp.on('data', (chunk) => {
data += chunk;
});
resp.on('end',(chunk) => {
let countries = JSON.parse(data).data;
for(let info of countries) {
const { alpha2Code } = info;
if(alpha2Code.localeCompare(code)===0){
const { name } = info;
console.log(name); //Getting the correct result here
return name; //here I am getting undefined
};
};
});
});
}
}
I am a beginner to javascript please help.