Tuesday 15 February 2011

javascript - Efficient way to convert units from one system to another -


I have to face a difficult time so that different types of units can be converged to different types.

Switch cases will work, but IMO is not in an effective manner. I have 3 different systems (SI, Imperial and US).

My converter (inside my app) will always be converted from SI to some or some of the SI, it's like less complexity, but still I still have advice on how to make a clean code.

I had defined that my input parameter for the conversion would be the value (the number) and the unit (string).
I will have 2 functions

The unit is defined because I am changing the length, weight, quantity etc.

What would you suggest?

/ P>

How about:

  Var converter = {'SEMO': function (val) {// implementation, some return}, 'kilogram': function (val) {// execution, make some return} //, etc.}}, 'Cetus': {'cm': function (val) {// implementation, some return}, 'kg': function (val) {// execution, return something} //, etc.}, 'USToSI': { 'Cm': function (val) {/ * ... * /} // etc}, etc} SIToSomething (value, unit, system) {return converters ["Sito" + system] [unit] (value ); } Var meter inmapial = sitosome (100, 'cm', 'imperial'); Var panna kilkoscience = sitosoming (50, 'kg', 'US');    

No comments:

Post a Comment