0

I have the following code. I think either the return statement or the assignment isn't working. The output on the alert is NaN.

let currencyRate = getCurrencyRate();
let output = currencyRate * 22.50;
alert(output);

function getCurrencyRate() {
    let currencyRate = "";
    $.ajax({
        url: "http://free.currencyconverterapi.com/api/v5/convert?q=USD_INR&compact=y",
        dataType: "json",
        success: function(json) {
            currencyRate += json.USD_INR.val;
            return currencyRate;
        }
    })
}
NoobCoder
  • 189
  • 1
  • 14
  • You're returning from the success callback, not from `getCurrencyRate()`. Since AJAX is asynchronous, `getCurrencyRate()` returns when the request is sent, it doesn't wait for the response. – Barmar Dec 15 '18 at 06:36
  • let currencyRate = setTimeout(function(){ getCurrencyRate(); let output = currencyRate * 22.50; console.log(output); },1000) – Deepika Rao Dec 15 '18 at 07:05
  • Thanks Deepika but I was able to solve the problem using the callback method. – NoobCoder Dec 15 '18 at 07:40

0 Answers0