/    Sign up×
Community /Pin to ProfileBookmark

Dynamic calculation using value recovered from an api

I’m making a page that needs to have a Bitcoin quote.

On this page I need 2 inputs one that brings the price of bitcoin and another that brings the price in reais.
Ex: 1 Bitcoin costs xxx, xx (default value)

But when entering $ 100.00 in the real field he needs to bring the price equivalent to the amount of Bitcoins that is worth $ 100.00
The same for Bitcoin, if I enter a fraction it would need to bring the value in Reais.

I found a site that does this (https://dolarhoje.com/bitcoin-hoje/), but I wanted something for my page like
I got here, but it’s not doing like the site above.

HTML:
<body>
<div>
<input id=”currency” onBlur=”calcBitCoinValue()”/>
<input id=”bitcoin”/>
</div>
</body>

JAVASCRIPT:

let currentBitcoinValue = 0;
let currentCurrencyValue = 0;

function fetchBitcoinTickerJSON() {
const url = “https://www.mercadobitcoin.net/api/btc/ticker“;

fetch(url)
.then(response => response.json())
.then(data => {
currentBitcoinValue = data.ticker.buy;
});
}

function init() {
fetchBitcoinTickerJSON();
}

function calcBitCoinValue() {
const bitCoin = document.getElementById(“bitcoin”);
const realValue = parseFloat(document.getElementById(“currency”).value);

bitCoin.value = realValue/currentBitcoinValue;
}
init();

Could they help?

to post a comment
JavaScript

7 Comments(s)

Copy linkTweet thisAlerts:
@SempervivumJan 23.2021 — Your code works fine for me after I entered it into a test file, however there was a garbadge character visible in the first line `let currentBitcoinValue = 0;</C> in the keyword <C>let`. Maybe this makes your script crash.
Copy linkTweet thisAlerts:
@biakellyauthorJan 23.2021 — In fact it does not work correctly, when typing 1, input 2 brings the bitcoin value, but when typing a fraction example: 0.5 bitcoin, instead of half the price it adds up.

It should work just like this site: https://dolarhoje.com/bitcoin-hoje/

I did the first step but the calculation is incorrect.
Copy linkTweet thisAlerts:
@SempervivumJan 24.2021 — Unfortunately I cannot reproduce that:

1 results in 0.000005571096438576322

0.5 results in 0.000002785548219288161

What do you mean "instead of half the price it adds up."?
Copy linkTweet thisAlerts:
@JennieMillerJan 25.2021 — your code is good.
Copy linkTweet thisAlerts:
@VITSUSAJan 25.2021 — I agree with biakelly, this code does not work correctly.
Copy linkTweet thisAlerts:
@johncarryJan 28.2021 — code is working.
Copy linkTweet thisAlerts:
@biakellyauthorJan 31.2021 — @VITSUSA#1627143

Exactly, it should work with the same logic as this site:

[https://dolarhoje.com/bitcoin-hoje/](url)
×

Success!

Help @biakelly spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.8,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...