Skip to content

Instantly share code, notes, and snippets.

View matheus-rossi's full-sized avatar
🎯
Data Engineer

Matheus Rossi matheus-rossi

🎯
Data Engineer
View GitHub Profile
const convertCurrencyAlt = async (from, to, amount) => {
const countries = await getCountriesAlt(to)
const rate = await getExchangeRateAlt(from, to)
const exchangedAmount = amount * rate
return `${amount} ${from} is worth ${exchangedAmount} ${to}. ${to} can be used in the following countries: ${countries.join(', ')}`
}
const convertCurrency = (from, to, amount) => {
let countries
return getCountries(to).then((tempCountries) => {
countries = tempCountries
return getExchangeRate(from, to)
}).then((rate) => {
const exchangedAmount = amount * rate
return `${amount} ${from} is worth ${exchangedAmount} ${to}. ${to} can be used in the following countries: ${countries.join(', ')}`
})
}
const getCountries = (currencyCode) => {
return axios.get(`https://restcountries.eu/rest/v2/currency/${currencyCode}`).then((res)=> {
return res.data.map((country)=> country.name)
})
}
const getExchangeRate = (from, to) => {
return axios.get(`http://api.fixer.io/latest?base=${from}`).then((res)=> {
return res.data.rates[to]
})
}