Calculation in function : Function Definition « Function « JavaScript Tutorial






<html>
<head>
<title>A Simple Page</title>
<script language="JavaScript">
<!--
function inputCels()
{
    var cels = 100;
    ansFah = doFahCalc(cels);
    alert(cels + " Degrees Celsius is " + ansFah + " Degrees Fahrenheit");
}

function inputFah()
{
    var fah = 100;
    ansCel = doCelCalc(fah);
    alert(fah + " Degrees Fahrenheit is " + ansCel + " Degrees Celsius");
}

function doCelCalc(fah)
{
    var ans = ((Number(fah) - 32) / 1.8);
    return (ans);
}

function doFahCalc(cels)
{
    var ans = ((1.8 * Number(cels)) + 32);
    return (ans);
}
//  -->
</script>
</head>
<body>
<input type="button" value="Convert Celsius to Fahrenheit" onClick="inputCels();">
<br>
<br>
<input type="button" value="Convert Fahrenheit to Celsius" onClick="inputFah();">
</body>
</html>








7.1.Function Definition
7.1.1.Functions
7.1.2.Define the simplest function
7.1.3.Define a function
7.1.4.Calculation in function
7.1.5.Recursive function