Return 2 Values from Function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike Copeland

    Return 2 Values from Function

    In the following code I am calculating 2 values when the "Compute
    Distance" button is pressed. I would like to find a way to "return" both
    value to the form so I can show both when the calculation is made. (one
    is merely the doubling of the computed result.)
    I have cobbled this form from several places, and I don't remember
    enough of HTML or JavaScript to know where to seek additional
    information. The code below does the calculation, but the "alerts" have
    been my attempt to see what's being done in the function. I have not
    actually returned any value to the form - which is my current dilemma: I
    can display one value, but I want to display two.
    Any thoughts? TIA
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
    <HTML><HEAD><TI TLE>Distances Calculator</TITLE>
    <script type="text/javascript">
    <!--
    var Total = 0.0;
    function roundOff(value, precision)
    {
    value = "" + value //convert value to
    string
    precision = parseInt(precis ion);

    var whole = "" + Math.round(valu e * Math.pow(10, precision));
    var decPoint = whole.length - precision;

    if(decPoint != 0)
    {
    result = whole.substring (0, decPoint);
    result += ".";
    result += whole.substring (decPoint, whole.length);
    }
    else
    {
    result = whole;
    }
    return result;
    } // roundOff

    function Compute(S,E)
    {
    Distances = new Array(3.90, 4.00, 0.76, 0.48);
    var start = S.selectedIndex ;
    var end = E.selectedIndex ;
    for (var I = 0; I < Distances.lengt h; I++)
    Total = Total+Distances[I];
    alert ("Starting @ "+start+" and Ending @ "+end);
    Total = 0;
    if (start < end)
    for (var I = start; I < end; I++)
    {
    Total = Total+Distances[I];
    }
    else
    for (var I = start; I > end; I--)
    {
    Total = Total+Distances[I];
    }
    Total = roundOff(Total, 2);
    alert ("Distance = "+Total);
    alert("Distance = "+Total+" miles;
    Loop = "+(Total*2) +" miles");
    return Total;
    } // Compute
    -->
    </script></HEAD>

    <BODY leftmargin="0" topmargin="0" marginheight="0 " marginwidth="0"
    bgcolor="#fffff f" link="#01A9B5" alink="#01A9B5"
    vlink="#829899" >

    <!-- MAIN BODY BEGINS -->
    <BODY bgcolor="#fffff f"><A NAME="top">
    <TABLE WIDTH=100% BORDER=0>
    <tr><TD>&#160;& #160;&#160;</TD></tr>
    <tr><TD>&#160;& #160;&#160;</TD></tr>
    <TR>
    <TD>&#160;&#160 ;&#160;</TD>
    <TD VALIGN="top">
    <font size=5 face="Arial, Helv"color="#66 0066">
    <center><B>Dist ance Calculator</B></center>
    </font>
    <font size="4" face="Arial, Helv" color="BLUE">
    <center>
    Use this calculator to compute distance from one point to another.<P>
    </font>
    <TABLE BORDER=8>
    <TR>
    <TD>
    <TABLE BORDER=0>
    <TR>
    <TD ALIGN="left"><h 3>
    <form name="Start">
    &nbsp;Start of Run:&nbsp;&nbsp ;&nbsp;&nbsp;&n bsp;&nbsp;&nbsp ;
    &nbsp;
    <SELECT name="Start_Poi nt">
    <OPTION value="0">Grani te Reef
    <OPTION value="1">Highw ay 87
    <OPTION value="46">Gree nway
    <OPTION value="47">West ern End
    </SELECT>
    &nbsp;&nbsp;<in put type="reset" value="Reset Start">
    </form>
    </TD>
    </TR>
    <TR>
    <TD ALIGN="left">
    <form name="End"><h3>
    &nbsp;End of Run/Loop:
    <SELECT name="End_Point ">
    <OPTION value="0">Grani te Reef
    <OPTION value="1">Highw ay 87
    <OPTION value="46">Gree nway
    <OPTION value="47">West ern End
    </SELECT>
    &nbsp;&nbsp;<in put type="reset" value="Reset End&nbsp;"><P>
    <input type="button" value="Compute Distance"
    onClick="Comput e(document.Star t.Start_Point,
    document.End.En d_Point)">
    &nbsp;&nbsp;
    <input type="input" name="answer" size=11>&nbsp;& nbsp;&nbsp;Mile s<BR>
    </form>
    </TD>
    </TR>


    </TABLE>
    </TD>
    </TR>
    </TABLE>
    </TD>
    </TR>
    </TABLE>
    <NOSCRIPT>
    Your browser doesn't support JavaScript, so this feature will not
    function.
    Consider upgrading to a more modern browser.
    </NOSCRIPT>
    <!-- MAIN BODY ENDS -->
    </BODY></HTML>
Working...