Inbuilt function:

1.  ParseInt():
This function is used to convert a string value to an integer. This
function returns the first integers contained in a string or NaN value. If the
string doesn’t begin with an integer.   
2.  Parsefloat():
This function returns the first floating point number contained in a
string or NaN value. If the string does not begin with a decimal number.
e.g.
alert(“sum is” + (parsefloat(num1) + (parsefloat(num2));

User Defined Function:
Syntax:
function functionname(argument list)
{
    block;
}  
A user defined function can be declared
inside the head or the body section. A user defined function needs to be
declared first before it is called. The function can have any number of
argument and may return a value.
e.g.
<html> <head> <title>HTML Program </title>
  <script >
     function calculate()
      {
var n1=prompt("Enter first integer");
        var n2=prompt("Enter second
integer"
);
       alert("Sum
is "
+ (parseInt(n1)+parseInt(n2)));
      }
  </script>
</head>
<body>
<br><br>
  <form>
    <input type=button value="CALCULATE
SUM"
style="font-size:18;font-family:candara;font-weight:bold;border-style:solid;border-color:blue;border-width:5;" onClick="calculate();">
</form> </body> </html>

Output:
e.g.
<html>
<head>
  <title>HTML Program</title>
  <script >
    function calculate(n1,n2)
      {
alert(
"Product is " + (n1*n2));
      }
  </script>
</head>
<body >
<br><br>
 <form>
<table style="border-style:solid;border-color:aqua;border-width:5;" cellpadding=11
cellspacing=11>
<tr style="font-size:22;">
    <td>Enter first integer </td>
    <td><input
type=text name=txt1>
</tr>
<tr style="font-size:22;">
    <td>Enter second integer </td>
    <td><input
type=text name=txt2>
</tr>
<tr colspan=2>
<td align=center><input type=button value="Calculate Product" style="font-size:22;" onClick="calculate(txt1.value,txt2.value);" ></td>
</tr>
</table> </form> </body> </html>
Output:

Post a Comment

0 Comments