lundi 23 février 2015

Alternative to hundreds/thousands of if statements

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


Aucun commentaire:

Enregistrer un commentaire