Hi guys
I've been learning JavaScript and I tried a simple little calculator thing. I know that JavaScript has a calculator code thing built in, (see http://www.javascriptkit.com/script/cut18.shtml) but I wanted to test my skills with this way.
Here is my HTML:
************
<html>
<head>
<title>My Calculator!!!</title>
<script language="JavaScript" src="calculator.js"></script>
</head>
<body>
<center>
<br>
<br>
<br>
<br>
<br>
<br>
<form name="calculator">
<input type="text" name="firstValue" maxlength="10">
<input type="button" name="plus" value="+" onclick="plus()"> / <input type="button" name="minus" value="-" onclick="minus()"> / <input type="button" name="multiply" value="X" onclick="multiply()"> / <input type="button" name="divide" value="÷" onclick="divide()">
<input type="text" name="secondValue" maxlength="10">
<input type="button" name="equals" value="=" onclick="calculate()">
</form>
</body>
</html>
************
And here is my JavaScript
************
//declaring variables
//getting first & second values from the form & parsing the integers
var firstValue = document.forms['calculator'].firstValue....
firstValue = parseInt(firstValue);
var secondValue = document.forms['calculator'].secondValue...
secondValue = parseInt(firstValue);
//variables to hold whether plus, minus etc are activated
var plus =0;
var minus =0;
var multiply =0;
var divide =0;
//the result of the calculation
var result;
//these will change the operation on the click of the plus, minus etc buttons
function plus(){
plus =1;
}
function minus(){
minus =1;
}
function multiply(){
multiply =1;
}
function divide(){
divide =1;
}
//the calculation function. basically, does the operation on condition that that operation has been selected. then displays result in an alert.
function calculate(){
if(plus == 1){
result = firstValue + secondValue;
alert("The answer is " +result);
}
if(minus == 1){
result = firstValue - secondValue;
alert("The answer is " +result);
}
if(multiply == 1){
result = firstValue * secondValue;
alert("The answer is " +result);
}
if(divide == 1){
result = firstValue / secondValue;
alert("The answer is " +result);
}
}
***********
It doesn't seem to do anything, and I can't seem to see anything wrong with it.
Your help would be much appreciated!
-James =)
Tags: