Skip to content

Instantly share code, notes, and snippets.

@mrbusche
Created October 27, 2021 22:59
Show Gist options
  • Save mrbusche/8d4077a20b008e787bdab512a615ec92 to your computer and use it in GitHub Desktop.
Save mrbusche/8d4077a20b008e787bdab512a615ec92 to your computer and use it in GitHub Desktop.
cryptos
<script>
const cryptos = [
{ type: 'SHIB', amount: 1311286.32422093 },
];
const formatter = new Intl.NumberFormat('en-US', {
style: 'currency',
currency: 'USD',
});
function getTotals(cryptos) {
let total = 0;
cryptos.forEach((crypto) =>
fetch(`https://min-api.cryptocompare.com/data/price?fsym=${crypto.type}&tsyms=USD`)
.then((response) => {
return response.json();
})
.then((data) => {
console.log([crypto.type, data.USD]);
const cryptoTotal = crypto.amount * data.USD;
total += cryptoTotal;
// console.log(['total from for loop', formatter.format(total)]);
console.log([`total for ${crypto.type}`, formatter.format(cryptoTotal)]);
return formatter.format(total);
})
);
return total;
}
// const total = await getTotals(cryptos);
const total = getTotals(cryptos);
console.log(['total', total]);
</script>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment