Basic HTML Version

|  | Table of Contents |  | View Full Version

Page 51 - 2611_SmartGPT Pro V(5.0) C-8
P. 51

    If you want to perform mathematical calculations, you need to convert the value into an integer
                     by using the parseInt( ) method and into a floating-point number by using the parseFloat( )
                     method. We can use the parseInt( ) method in the following way:

                     var num = parseInt(prompt("Enter first number: "));
                     The document.write( ) method is used to display output on the web page.
                     For example:

                     document.write("Hello from JavaScript");
                     The preceding statement will display the message "Hello from JavaScript" on the web page. Let us
                     create a web page to calculate the sum of two numbers in JavaScript.

                     <HTML>
                     <HEAD>
                     <TITLE> Statements in JavaScript </TITLE>
                     </HEAD>
                     <BODY>
                     <SCRIPT type="text/javascript">
                     var a = parseInt(prompt("Enter first number: "));
                     var b = parseInt(prompt("Enter second number: "));
                     var c;
                     c = a + b;
                     document.write("The sum is: ");
                     document.write(c);
                     </SCRIPT>
                     </BODY>
                     </HTML>
                     Save the file with .html extension and open the web page in web browser. You will get the following
                     output:

                                                                                          Dynamic Web Pages in HTML5  49

| 46 47 48 49 50 51 52 53 54 55 56 |