Skip to content

Instantly share code, notes, and snippets.

@pkakelas
Created May 13, 2020 23:17
Show Gist options
  • Save pkakelas/38b9ce24bd1e94b09ba3afea3ecb1bc8 to your computer and use it in GitHub Desktop.
Save pkakelas/38b9ce24bd1e94b09ba3afea3ecb1bc8 to your computer and use it in GitHub Desktop.
const rp = require('request-promise')
const baseUrl = 'https://api.covid19api.com/'
const {getCases, getCountryNames} = require('./request')
//request.js
const doMakeRequest = async (endpoint) => {
const res = await rp(`${baseUrl}${endpoint}`)
return JSON.parse(res)
}
const getCases = async (slug) => {
return doMakeRequest(`/live/country/${slug}/status/confirmed`)
}
const getCountryNames = async () => {
const countries = await doMakeRequest('/countries')
return countries.map(c => c.Slug)
}
const main = async () => {
const names = await getCountryNames()
const cases = names.map(getCases)
return avgDeaths(Promise.all(cases))
}
const avgDeaths = (countries) => {
const validCountries = countries.filter(country => country.length != 0)
const deaths = validCountries.map(c => c.pop().Deaths)
return avg(deaths)
}
//Utils.js
const sum = numbers => numbers.reduce((acc, num) => acc + num)
const avg = (numbers) => sum(numbers) / numbers.length
main.then(console.log)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment