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.