Page 282 - Web Applications (803) Class 12
P. 282

Output:


























                15.   Create a webpage with JavaScript code that displays the sum of 5 elements in an array using a while loop. The
                    JavaScript code should be in an external file “resource.js”.
               Ans.  HTML File code:


                   <html>
                   <head>

                       <title>Sum of Numbers</title>

                       <script src=”resource.js”></script>
                   </head>

                   <body>

                       <h1>Sum of Numbers</h1>
                       <p id=”Result”></p>

                   </body>

                   </html>
                   Resource.js code:

                   document.addEventListener(“DOMContentLoaded”, function () {

                       function calculateSum() {
                           var num = [6, 12, 21, 36, 40];

                           var sum = 0;

                           var i = 0;
                      while (i < num.length) {

                          sum += num[i];

                          i++;



                280   Touchpad Web Applications-XII
   277   278   279   280   281   282   283   284   285   286   287