- 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" | |
| } | |
| } |