UPDATE...RESOLVED: see note below
I'm trying to retrieve a crypto price from https://min-api.cryptocompare.com/data/price?fsym=HIVE&tsyms=USD then use it in a formula as a variable. Sounds simple and likely is, but I've tried several tutorials with no luck.
A working live version of the code is at http://crrdlx.websavvy.work/hivetiptokens/ or at https://codepen.io/crrdlx/pen/wvqNBWZ The problematic code is below. Note that const HIVEusd = 2;
the "2" is just a dummy for testing purposes. HIVEusd should be equal to the "USD" value retrieved from the API.
Thank you for any help and your patience. :)
<script language=javascript>
const toFix = str => (+str).toFixed(6); // perhaps should do 6 to show more precision?
const calc = $div => {
// Retrieve HIVE price in USD
$.getJSON("https://min-api.cryptocompare.com/data/price?fsym=HIVE&tsyms=USD", function(data) {
$("#HIVEPrice").text(toFix(data["USD"]));
calc($("#HIVEPrice"));
}).fail(function(dat, textStatus, error) {
var err = textStatus + ", " + error;
alert(err);
});
// End HIVE price retrieve
// Begin calculations for table of live values
const $row = $div.closest("tr")
const $cells = $row.find("td");
const qty = +$cells.eq(2).text().split(" ")[0];
const val = $div.text()
// console.log($div.attr("id"),val, qty, val * qty)
const res = val * qty
$cells.eq(3).text(isNaN(res) ? "" : res.toFixed(4)); // or (2) but then several will show 0.00
// figure the USD value: res (HIVE value) times HIVEusd equals token USD value
const HIVEusd = 2;
const valusd = res * HIVEusd;
$cells.eq(4).text(isNaN(valusd) ? "" : valusd.toFixed(4)); // or (4) for more precision in decimals
};
// End table price calculations