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

24.  Write a function in JavaScript that accepts two integer variables and returns their sum.   [2022 T2]
                       function add(n1,n2){

                           var sum=n1+n2;
                           return sum;
                       }
                       var result=add(10,12);
                       document.write("The sum is "+result);
                   25.  Explain the term inner function in JavaScript with an example.                           [2022 T2]
                   Ans.  JavaScript supports nested functions i.e., functions defined inside another function. Only statements in the outer
                       function can access the inner function. The inner function can make use of the arguments and variables of the
                       outer function. However, the outer function cannot make use of the inner function's arguments and variables.

                       <script>
                       function add() {
                         let ctr = 10;
                         function inc() {ctr=ctr-1;}
                         inc();  //call to inner function
                         return ctr;

                       }
                       document.write(add());   //call to outer function
                       </script>
                       Output: 9
                   26.  Explain the use of the reverse method in JavaScript. Write a function 'Revrs' in JavaScript to explain reverse the
                       order of elements in a given array.                                                       [2022 T2]
                   Ans.  The reverse() method in JavaScript is used to reverse the order of elements in an array. It modifies the original
                       array in place, meaning it changes the order of elements without creating a new array.
                       var Arr = [10, 20, 33, 44, 50];

                       document.write(Arr.reverse());
                       Output: 50,44,33,20,10
                   27.  Write a program in JavaScript that accepts three numbers from the user and displays the average
                       value.                                                                                    [2022 T2]

                   Ans.    <script>
                               // Prompt the user to enter three numbers
                               var num1 = parseFloat(prompt("Enter the first number:"));
                               var num2 = parseFloat(prompt("Enter the second number:"));
                               var num3 = parseFloat(prompt("Enter the third number:"));
                               // Calculate the average
                                   var average = (num1 + num2 + num3) / 3;
                                   // Display the result

                                   document.write("The average is "+ average);
                           </script>






                                                                                          Web Scripting—JavaScript   233
   230   231   232   233   234   235   236   237   238   239   240