This is a unit converter in JS. When someone converts volumes, the input volume first gets converted to litres, and then to every other unit. The computed values automatically get displayed in the input fields. For that purpose I created an object to store the converting values, like so:
var volumes={
cbm: 1000,
ltr: 1,
ml: 0.001,
barrel: 158.9873,
gal: 3.785411784,
pint: 0.473176473,
floz: 0.0295735295625,
//and so on
};
And for the conversion between the units of one system I tried this:
var volumes={
impfloz: 0.0284130642624675,
imppint: this.impfloz*20,
impbarrel: this.impfloz*5600,
impgal: this.impfloz*160
};
But it didn’t work. When I type in a value, e.g. in the input field “Imperial-Barrel”, all other input fields display: NaN. Do I really have to write functions for each of these? It would make the code much more complicated and more difficult to read. Is there a way to get the object members to automatically apply the desired mathematical operation?