Closed alijnmerchant21 closed 3 years ago
Hi!
I do not have a YouTube channel which explains the solutions, however, below is the explanation to the JavaScript Certification Country Codes problem.
function fetch(url) {
return new Promise((resolve, reject) => {
https
.get(url, (res) => {
let result = '';
res.on('data', (chunk) => {
result += chunk;
});
res.on('end', () => {
resolve(JSON.parse(result));
});
})
.on('error', (error) => {
reject(error);
});
});
}
Firstly, the res.on(data, callback)
is a way of listening for the events emitted by a function, in this case, the https.get
method. Since the https.get
does not return a promise, the above code is wrapping that function to instead return a promise. Promises are explained more in-detail here.
A promise has additional methods, as opposed to using event emitters for every HTTPS request. So, instead of doing the below for every HTTPS request:
https
.get('my-url', (res) => {
let result = '';
res.on('data', (chunk) => {
result += chunk;
});
res.on('end', () => {
const data = JSON.parse(result);
// do something with the data
});
})
.on('error', (error) => {
// do something with the error
});
This allows us to use a more streamlined approach to perform the HTTPS request. As that would instead allow us to run the below:
fetch('my-url')
.then(data => {
// handle the data
})
.catch(error => {
// handle the error
})
async function getCountryName(code) {
const { total_pages, data } = await fetch(
`https://jsonmock.hackerrank.com/api/countries?page=1`
);
const answer = findInCountries(data, code);
if (answer) return answer.name;
for (let i = 2; i <= total_pages; i++) {
const { data } = await fetch(
`https://jsonmock.hackerrank.com/api/countries?page=${i}`
);
const answer = findInCountries(data, code, i);
if (answer) return answer.name;
}
}
function findInCountries(countries, code, i = 1) {
return countries.find((c) => c['alpha2Code'] === code);
}
The main principle of the above is that it uses Object Destructuring to get the data in JSON format for page 1 and tries to find the property alpha2Code
that matches the code the user is searching for. If the result is not found, it will continue to search each page and returns the country name if it is found.
The above approach is only recommended for this solution, as the above could be simplified by using node-fetch
, and end up doing the same in fewer lines of code, as seen below.
const fetch = require('node-fetch');
async function getData(url) {
const res = await fetch(url);
return res.json();
}
The await
is being used to wait until the promise fetch(url)
is settled (either resolved or rejected), then it would need to be a bit longer for error handling with a try-catch block, similar to the one mentioned at the end of (1).
I hope this was of help!
Can you please explain how you have written this code.
If you have a Youtube channel, or some other resource - you can point me to it.
For this problem - https://github.com/nathan-abela/HackerRank-Solutions/blob/master/Skills%20Certification/JavaScript%20-%20Basic/Country%20Codes.js