Page 373 - Web_Application_v2.0_C12_Fb
P. 373

3.   Write JavaScript code to find the factorial of a number entered in a textbox on a webpage using functions.

                   Ans.  <!DOCTYPE HTML>
                      <HTML>
                      <HEAD>
                      <TITLE>Factorial Calculator</TITLE>
                      </HEAD>
                      <BODY>

                      <H2>Factorial Calculator</H2>
                      <SCRIPT>
                      function calculateFactorial() {
                      var number = document.getElementById("number").value;
                      var factorial = 1;
                      for (var i = 1; i <= number; i++) {
                      factorial *= i;
                      }
                      document.getElementById("result").innerHTML = "The factorial is "+factorial;
                      }

                      </SCRIPT>
                      <P>Enter a number to calculate its factorial:</P>
                      <INPUT TYPE="text" id="number">
                      <BUTTON ONCLICK="calculateFactorial()">Calculate Factorial</BUTTON>
                      <P ID="result"></P>
                      </BODY>
                      </HTML>
                       Output of the preceding code is as follows:


















                   4.   Write a JavaScript program to take user input for name and age, pass it to a function, and convert it into
                      an object.

                   Ans.  <!DOCTYPE HTML>
                      <HTML>
                      <HEAD>
                      <TITLE>Object Creator</TITLE>
                      </HEAD>
                      <BODY>

                                                                                                    Practical Work   371
   368   369   370   371   372   373   374   375   376   377   378