Practical 7

Download as pdf or txt
Download as pdf or txt
You are on page 1of 2

Web Designing and Internet Applications 1

Aim: - Write a JavaScript to design a simple calculator to perform the following


operations: sum, product, difference and quotient.

Tools: Notepad Editor and Web Browser like Internet Explorer, Google Chrome or Mozilla
Firefox.

 Standard Procedure for Creating and View a JavaScript document?


1. Use a text editor such as Notepad to write the document.
2. Save the file as filename.html on a PC. This is called the document Source.
3. Open the file that you have saved in any browser Off-Line.
4. Your HTML page should now appear just like any other Web page in browser.
5. You may now switch back and forth between the Source and the HTML
Document
 switch to Notepad with the Document Source
 make changes .
 save the document again.
 switch back to browser .
 click on RELOAD and view the new HTML Document .
 switch to Notepad with the Document Source.

 Coding:
<html>
<head>
<title>My calculator</title>
<script type="text/javascript"> function call(click_id)
{
Var v1=parseFloat(document.getElementById("ip1").value);
var v2=parseFloat(document.getElementById("ip2").value);
if(isNaN(v1) || isNaN(v2)) alert("enter a valid number");
else if(click_id=="add") document.getElementById("output").value=v1+v2;
else if(click_id=="sub") document.getElementById("output").value=v1-v2;
else if(click_id=="mul") document.getElementById("output").value=v1*v2;
else if(click_id=="div") document.getElementById("output").value=v1/v2;
}
</script>
</head>
Web Designing and Internet Applications 2

<body>
<center>
<h1> Simple Calculator Program</h1>
<table style="background-color:pink" align=="center">
<tr>
<td>
<form method="get" action="">
<div width=100% align="center">
<label>Op1<input type="text" id="ip1"/></label>
<label>Op2<input type="text" id="ip2"/></label>
<lablel>total<input type="text" id="output"/></label>
</div>
<br>
<div width=100% align="center">
<input type="button" value="+" id="add" onclick="call(this.id)"/>
<input type="button" value="-" id="sub" onclick="call(this.id)"/>
<input type="button" value="*" id="mul" onclick="call(this.id)"/>
<input type="button" value="/" id="div" onclick="call(this.id)"/>
<input type="reset" value="clear"/>
</div>
</form>
</td>
</tr>
</table>
</center>
</body>
</html>

Conclusions:
The JavaScript code is executed successfully and all the arithmetic operations (sum, product,
difference and quotient) have been performed.

You might also like