Javascript Unit Converter

1.7k Views Asked by At

This is a simple one, I'm new to javascript, but I haven't found any existing answers that can help with this:

I started with a simple calculator that would convert one unit, the Cent (a musical unit of note frequency difference) to a decimal.

<html>

    <head>
        <script language="JavaScript">
            function tun(form) {
                var i = parseFloat(form.Cents.value, 10);
                var o = 0;
                p = i / 3986.313714
                o = Math.pow(10, p)

                form.Decimal.value = o;
            }
        </script>
    </head>

    <body>
        <FORM>Cents:
            <INPUT NAME="Cents" VALUE="0" MAXLENGTH="15" SIZE=15>
            <p>Click to Convert
                <INPUT NAME="calc" VALUE="Calculate" TYPE=BUTTON onClick=tun(this.form)>
                <p>Decimal:
                    <INPUT NAME="Decimal" SIZE=15>
        </FORM>
    </body>

</html>

Now I want to change this to a converter for many types of units into each other, using the Decimal as a go-between. I'd like the 'input unit' and 'output unit' to be selectable from dropdown menus.

Here's where I am so far:

<html>

    <head>
        <script language="JavaScript">
            function tun(form) {
                var i = parseFloat(form.Cents.value, 10);
                var o = 0;
                p = i / 3986.313714
                o = Math.pow(10, p)
                form.Decimal.value = o;
            }
        </script>
        <script>
            function selectedunit() {
                var mylist = document.getElementById("InputList");
                document.getElementById("units").value = InputList.options[mylist.selectedIndex].text;
            }
        </script>
    </head>

    <body>
        <form>Select units to convert from:
            <select id="InputList" onchange="selectedunit()">
                <option></option>
                <option>Cents</option>
                <option>Savarts</option>
                <option>Jots</option>
                <option>Merides</option>
            </select>
            <p>Selected Units:
                <input type="text" id="units" </p>
                <br>Cents:
                <INPUT NAME="Cents" VALUE="0" MAXLENGTH="15" SIZE=15>
                <p>Click to Convert
                    <INPUT NAME="calc" VALUE="Calculate" TYPE=BUTTON onClick=tun(this.form)>
                    <p>Decimal:
                        <INPUT NAME="Decimal" SIZE=15>
        </FORM>
    </body>

</html>

So I'd like to be able to selected any input unit and any output unit, then enter a value and calculate away - a bit like a currency converter

Any help?

Jim

1

There are 1 best solutions below

1
On

Try and make two different javascript functions

  1. AnyUnitToDecimalConverter() Function
  2. DecimalToAnyUnitConverter() Function

Now suppose you need to convert from X unit to Y unit: Call AnyUnitToDecimalConverter(valueX), this will give you a decimal answer say AnsDecimal and now Call DecimalToAnyUnitConverter(AnsDecimal);