Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ethereum to USD / other currencies while typing in input field

I am not so good at JS, I have been battling with this code that should let me convert Ethereum to a selected Currency as i type value into input field, it does nothing and when i debug it, it seems to keep returning NaN with the error:

The specified value "NaN" is not a valid number. The value must match to the following regular expression: -?(\d+|\d+\.\d+|\.\d+)([eE][-+]?\d+)?

Below is my code, your help is appreciated greatly.

code:

 $(".currencyField").keyup(function(){ //input[name='calc']
 let convFrom;
 if($(this).prop("name") == "eth") {
       convFrom = "eth";
       convTo = "usd";
 }
 else {
       convFrom = "usd";
       convTo = "eth";
 }
 $.getJSON( "https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&ids=ethereum", 
    function( data) {
    var origAmount = parseFloat($("input[name='" + convFrom + "']").val());        
    var exchangeRate = parseInt(data.current_price);
    let amount;
    if(convFrom == "eth")
       amount = parseFloat(origAmount * exchangeRate);
    else
       amount = parseFloat(origAmount/ exchangeRate); 
    $("input[name='" + convTo + "']").val(amount.toFixed(2));
    price.innerHTML = amount
    });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="number" name="eth" class="currencyField" placeholder="ETH">
      <div class="arrow" style="margin: 0 10px";>=</div>
      <input type="number" name="usd" class="currencyField" placeholder="USD">
    </div><span id="price"></span>
like image 959
Basil Moses Avatar asked Oct 22 '25 13:10

Basil Moses


1 Answers

If you look at the response for the api, it looks like it returns an array. So to access the the current_price you would need to reference the array's index:

var exchangeRate = parseInt(data[0].current_price);

Full code below:

 $(".currencyField").keyup(function(){ //input[name='calc']
 let convFrom;
 if($(this).prop("name") == "eth") {
       convFrom = "eth";
       convTo = "usd";
 }
 else {
       convFrom = "usd";
       convTo = "eth";
 }
 $.getJSON( "https://api.coingecko.com/api/v3/coins/markets?vs_currency=usd&ids=ethereum", 
    function( data) {
    var origAmount = parseFloat($("input[name='" + convFrom + "']").val());        
    var exchangeRate = parseInt(data[0].current_price);
    let amount;
    if(convFrom == "eth")
       amount = parseFloat(origAmount * exchangeRate);
    else
       amount = parseFloat(origAmount/ exchangeRate); 
    $("input[name='" + convTo + "']").val(amount.toFixed(2));
    price.innerHTML = amount
    });
});
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<input type="number" name="eth" class="currencyField" placeholder="ETH">
      <div class="arrow" style="margin: 0 10px";>=</div>
      <input type="number" name="usd" class="currencyField" placeholder="USD">
    </div><span id="price"></span>
like image 80
Austin Greco Avatar answered Oct 25 '25 05:10

Austin Greco