Skip to content

Instantly share code, notes, and snippets.

@himelnagrana
Last active August 20, 2020 05:54
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save himelnagrana/0af367d4beebdb79bfe8333c717b89fd to your computer and use it in GitHub Desktop.
Save himelnagrana/0af367d4beebdb79bfe8333c717b89fd to your computer and use it in GitHub Desktop.
  • npm install
  • node index.js ba 1000000

Result will be 13

const axios = require('axios');
const countries = { count: 0 };
async function getCountries(s, p) {
await fetchAPI(s, p);
}
async function fetchAPI(str, population, page = 0) {
const resp = await axios.get(`https://jsonmock.hackerrank.com/api/countries/search?name=${str}&page=${page}`);
resp.data.data.map(country => {
if (country.population > population) {
countries.count++;
}
});
if ((resp.data.total_pages > 1) && (page <= resp.data.total_pages)) {
page++;
await fetchAPI(str, population, page);
} else {
console.log(countries.count);
}
}
// running from command line
Promise.resolve(getCountries(process.argv[2], process.argv[3]));
{
"name": "fetchcountries",
"version": "1.0.0",
"description": "hacker rank test for greator",
"main": "index.js",
"author": "Himel Nag Rana <hnrana@gmail.com>",
"private": true,
"dependencies": {
"axios": "0.19.0"
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment