Skip to content

Instantly share code, notes, and snippets.

@fernandodof
Last active February 20, 2021 19:10
Show Gist options
  • Save fernandodof/8604b452dcde6b66888f7690b38b60ab to your computer and use it in GitHub Desktop.
Save fernandodof/8604b452dcde6b66888f7690b38b60ab to your computer and use it in GitHub Desktop.
async function getPricesLastDays(days) {
try {
const result = await fetch(`https://api.gold/price?days=${days}`);
const prices = await result.json();
const sum = prices.reduce((total, currentValue) => total + currentValue, 0);
const average = sum / prices.length;
return { average: +average.toFixed(2) };
} catch (error) {
// handle error
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment