2

I'm writing a currency conversion app in html5 and javascript and hard coding the conversion rates. The problem is that there are 33 currencies i want to be able to convert between. this creates a problem as the only way i can think of doing it is using if statements, this would mean that i would have to write 1056 if statements. i.e.

if (homeCountry = "uk" and destinationCountry = "france") {
    rate = 1.32;
}
if (homeCountry = "uk" and destinationCountry = "japan") {
   rate = 183.79;
}

For all uk conversions and then

if (homeCountry = "japan" and destinationCountry = "france") {
   rate = 0.0074;
}
if (homeCountry = "japan" and destinationCountry = "china") {
    rate = 0.053;
}

For all japanese converions etc.

Even using switch statements this would still be a huge amount of code to type. I was wondering if anyone could think of a solution to this to make it slightly less tedious. i've been trying to think of a solution for a while now and i'm stuck. Can anyone help? Thanks

Seank462
  • 51
  • 3
  • 8
    How about a 2d json object such as `var rates = {uk:{japan:183.79, france: 1.32}, japan:{france: 0.0074, china: 0.053}}` then use `rate = rates[homeCountry][destionationCountry]`; – Dave Goten Feb 23 '15 at 16:50
  • 2
    Well, it's a simple matrix of 33x33 - so you could just store all the conversions in a table and lookup based on the monetary unit (I wouldn't use "country" if I were you). Of course, you can easily remove half of the values, since `A -> B` is just inverse of `B -> A`. And all the currencies are convertible to one another, so you can use one currency as the reference value, and only store 33 conversions from this reference price to each of the others. – Luaan Feb 23 '15 at 16:51
  • 1
    @Luaan using a reference currency would definitely be the best solution – howderek Feb 23 '15 at 16:55
  • If you are intending this for any kind of widespread use, I'd also consider using the official ISO country codes, rather than names like "japan" and "france" ("JP" and "FR", respectively): https://www.iso.org/obp/ui/#search/code/ – talemyn Feb 23 '15 at 16:58

4 Answers4

5

why not create a kind of hash map like

  conversions: {
    uk: {
      france: 1.12,
      japan: 2.12
    },
    france: {
      uk: 1.22,
      japan: 2.3
    },
    japan: {
      france: 2.2,
      uk: 3.4
    }
  }

then you can access by going conversions[homeCountry][destinationCountry].

Andy
  • 61,948
  • 13
  • 68
  • 95
Quince
  • 14,790
  • 6
  • 60
  • 69
  • 1
    well i feel a little bad as after posting this i saw @Dave Goten wrote a comment with pretty much the same thing – Quince Feb 23 '15 at 16:52
  • 2
    typing speed should not be something that worries you. its not a typing contest, its a help forum – Sharky Feb 23 '15 at 16:54
  • This has a lot of duplicate data in it and is fraught with potential errors when maintaining. It seems it would make more sense to have each conversion rate to one common currency (probably to U.S. dollar or to Euro) and then you can calculate the rate from any one currency to another by first converting to the common currency. This will only ever have each currency listed once then with no possibility for conflicting, dual-maintained data. – jfriend00 Feb 23 '15 at 17:34
5

Create an object that will contain rates with regards to usd only

var rates = {
  usd: 1,
  eur: 0.77,
  gbp: 0.63
}

Then create a function like this:

function convertRate(src, dest, val){
  if(src=='usd'){
    return val*rates[dest];
  }
  else{
    return val * 1/rates[src] * rates[dest];
  }
}

Let's test!

convertRate('usd','eur',130);
convertRate('eur','gbp',130);

Doing the same without a function, with just a single 2d array has somewhat better performance, memory consumption is not an issue, but populating 2d array like this is tedious. Doing this in the way I suggested is easier and I think you won't notice any performance difference.

Orif Khodjaev
  • 1,080
  • 1
  • 13
  • 34
2

Perhaps you could do it using a bidimensional array that's 33x33 in size, so for instance if you access the position (4,10) you would be getting the ratio between the 4th and 10th language.

Another way would be to use an intermediate currency, if that's possible: first converting from initial currency to dollars and then from dollars to the target currency.

Hope this helps.

jbernardo
  • 159
  • 7
0

use objects:

hardcoded_data =
{
    'uk' : { 'france' : '1.32', 'japan' : '183.79' },
    'japan' : { 'france' : '0.0074', 'japan' : '0.053' }
};
Sharky
  • 6,154
  • 3
  • 39
  • 72