0

i am trying to move my code from many if statements like this one...

if (localStorage.getItem("gbpUSD") === null) {   

    $http({

        method: 'GET',
        url: "http://devel.farebookings.com/api/curconversor/GBP/USD/1/"
    }).

    success(function(status) {
    // your code when success
    localStorage.gbpUSD = status.substring(4);

    });   
}

To a function like this one...

// Get exchange rate from API - a=from, b=to
function getEXRate(a, b) {

    // Access API
    $http({

        method: 'GET',
        url: "http://devel.farebookings.com/api/curconversor/"+ a +"/"+ b +"/1/"
    }).
    // Check if successful
    success(function(status) {
    // Return ammount only
    return status.substring(4);

    });

}

and then just call the function each time...

// Check if null, if so get exchange rate
if (localStorage.getItem("gbpUSD") === null) { 

    // Access API to get exchange rate
    localStorage.gbpUSD = getEXRate('GBP', 'USD');
}

the if statement works and give me a number like 1.543, however the function is returning undefined

Any ideas why???

Thanks

CarlRyds
  • 217
  • 1
  • 5
  • 19
  • i have read that and still don't understand :/ – CarlRyds Nov 24 '15 at 14:38
  • Anything specific you still don't understand? I suggest reading the answer more than once if necessary. Let's start simple: what do you think the following function (`foo`) returns, and why, when it is called: `function foo() { function bar() { return 42; } }`. – Felix Kling Nov 24 '15 at 14:49

0 Answers0