0

I have a function rate_against_usd() which is supposed to loop through the rates section of the Open Exchange Rates API and find me a rate based on currency code.

//Get rate against USD
function rate_against_usd(currency_code) {
    $.getJSON('spaces/currency.json', function (loadedData) {
        // Run through the codes and find matching
        $.each(loadedData.rates, function (key, val) {
            if (key == currency_code) {
                return val;
            }
        });

    });
}

Here's how I'm using it

//Update Selected Currency
    selected_currency = $("#currency-select").val();
    // => 'JPY'

//Define Rate against USD (of selected currency)
    var rate_against_USD_selected = rate_against_usd(selected_currency);
    // => undefined

I've checked the JSON for loadedData.rates and it comes out like I expect

Key: Value
PGK: 2.721801
PHP: 44.47133
PKR: 102.0204
PLN: 3.592578
PYG: 5055.411628
QAR: 3.640315
etc

What am I doing wrong here? I'm stumped.

  • http://jsfiddle.net/Luxelin/3aeayga4/ (had been writing an answer when it was closed—forgot about the canonical) – royhowie May 19 '15 at 01:18
  • I think the main problem here is that the `return val` is actually returning from the each function. Check here: http://jsfiddle.net/1dgLftkf/. I don't think this should have been closed. – Devin H. May 19 '15 at 01:19

0 Answers0