Use function to calculate average in JavaScript
Description
The following code shows how to use function to calculate average.
Example
<html>
<head>
<script language="JavaScript">
function doCalcAvg(inpNum1, inpNum2, inpNum3, inpNum4)
{<!-- ww w. ja v a 2s. co m-->
var ans;
ans = (Number(inpNum1) + Number(inpNum2) + Number(inpNum3) + Number(inpNum4)) / 4;
return (ans);
}
var inpNum1 = 1;
var inpNum2 = 2;
var inpNum3 = 3;
var inpNum4 = 4;
numAvg = doCalcAvg(inpNum1, inpNum2, inpNum3, inpNum4);
document.write("The average of the four numbers you entered is: " + numAvg);
</script>
</head>
</html>
The code above generates the following result.
Javascript Tutorial Definition
Call a function defined later in JavaScript
Call a function in hyperlink in JavaScript
Call function inside a function in JavaScri...
Call your function in JavaScript
Call your function in body onLoad event in ...
Convert function to string in JavaScript
Create a function in JavaScript
Create a function variable in JavaScript
Create a function with Function constructor...
Use Bind to create a new new function whose...
Use apply function to call a function in Ja...
Use function as variable in JavaScript
Use function.call() to call a function in J...
Use function caller property in JavaScript
Use function reference value passing in Jav...
Use this to reference function context in J...
Use typeof to check function variable in Ja...
Call a function defined later in JavaScript
Call a function in hyperlink in JavaScript
Call function inside a function in JavaScri...
Call your function in JavaScript
Call your function in body onLoad event in ...
Convert function to string in JavaScript
Create a function in JavaScript
Create a function variable in JavaScript
Create a function with Function constructor...
Use Bind to create a new new function whose...
Use apply function to call a function in Ja...
Use function as variable in JavaScript
Use function.call() to call a function in J...
Use function caller property in JavaScript
Use function reference value passing in Jav...
Use function to calculate average in JavaSc...
Use same name for different functions in Ja...Use this to reference function context in J...
Use typeof to check function variable in Ja...