|
|
4.1 Arithmetic in JavaScript
JavaScript is not just a scripting
language, it can also perform arithmetic calculations! Best of all, it can
interact with online users by receiving data from them, operate on the data dan
then display the outputs to the users. The four basic arithmetic operators in
JavaScript is just the same as in VB, namely
| Addition |
+ |
| Subtraction |
- |
| Multiplication |
x |
| Division |
/ |
Lets
construct our first arithmetic program in JavaScript. You can either use a text
editor such as notepad to type in the statements or you can use Microsoft
Frontpage or Dreamweaver to do the job. Now, type in the following lines of
statements.
<HTML>
<HEAD>
<Script
Language="JavaScript">
var
Number1, // this is the first string input by
the user
Number2 , // this is the second string input
by the user
Num1 , // numeric value converted from Number1
Num2, // numeric value converted from Number2
sum,
difference ,
product,
quotient;
Number1=window.prompt("Enter your first Integer","0");
Number2=window.prompt("Enter your second integer","0");
Num1=parseInt(Number1);
Num2=parseInt(Number2);
sum=Num1+Num2;
difference=Num1-Num2;
product=Num1*Num2;
quotient=Num1/Num2;
document.writeln(Number1+"+"+Number2+"="+sum+"<BR>");
document.writeln(Number1+"-"+Number2+"="+difference+"<BR>");
document.writeln(Number1+"x"+Number2+"="+product+"<BR>");
document.writeln(Number1+"/"+Number2+"="+quotient);
</Script>
|