Page 213 - Web_Application_v2.0_C12_Fb
P. 213

Example 23: To display the average of all numbers in an array
                    <!DOCTYPE HTML>
                    <HTML>
                    <HEAD>
                    <TITLE>JavaScript program to Calculate Average</TITLE>
                    <BODY>
                    <H2>JavaScript Functions</H2>
                    <P>Function to display the average of all numbers of an array</P>
                    <SCRIPT>
                    function Display_avg(arr1) {
                    var sum=0;
                    var leng=arr1.length;
                    for(i=1;i<leng;i++)
                    sum=sum+arr1[i];
                    return(sum/leng)
                    }
                    const arr1=[18,26,45,34,78];
                    var ans=Display_avg(arr1);
                    document.write("The average is " +ans);
                    </SCRIPT>
                    </BODY>
                    </HTML>
                 Output:


















                 Example 24: To create a MyFunc( ) function  that  takes the user name as input and displays a message

                    <!DOCTYPE HTML>
                    <HTML>
                    <HEAD>
                    <TITLE>Display User Name</TITLE>
                    </HEAD>
                    <BODY>
                    <H2>Welcome Message Program</H2>
                    <SCRIPT>
                    // Function to take user name and display a message
                    function MyFunc() {
                    // Take user input
                    var userName = prompt("Enter your name:");
                                                                                                  JavaScript Part 2  211
   208   209   210   211   212   213   214   215   216   217   218