Webbers Corner

Full Version: Javascript Calculator
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Just made this calculator for an assigment at uni, was just woundering if people could check it out, try and brake it. make it do unexpected things.

cheers Big Grin

Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1" />
<title>Javascript Calculator</title>
<script language="JavaScript">
<!-- Begin
// if the number is 0, the new character will replace it, if it is not 0 then it will add it to the form.
function addChar(input, character) {
if(input.value == null || input.value == "0")
input.value = character
else
input.value += character
}

//Deletes the last digit in the form field.
function deleteChar(input) {
input.value = input.value.substring(0, input.value.length - 1)
}


//Changes the sign from - to + or + to -
function changeSign(input) {
if(input.value.substring(0, 1) == "-")
input.value = input.value.substring(1, input.value.length)
else
input.value = "-" + input.value
}

//Calculates the equation in the input field.
function compute(form)  {
form.display.value = eval(form.display.value)
}

//Checks to see if the numbers/characters added into the input field are numbers or mathmatical symbals.
function checkNum(str)  {
for (var i = 0; i < str.length; i++) {
var ch = str.substring(i, i+1)
if (ch < "0" || ch > "9") {
if (ch != "/" && ch != "*" && ch != "+" && ch !="-" && ch != ".") {
alert("Invalid Entry!")
return false
   }
    }
    }
return true
}
// End -->
</script>

</head>

<body>
<form>
<input name="display" value="0" size=25>
<br />
<input type="button" value=" 7 " onClick="addChar(this.form.display, '7')">
<input type="button" value=" 8 " onClick="addChar(this.form.display, '8')">
<input type="button" value=" 9 " onClick="addChar(this.form.display, '9')">
<input type="button" value=" / " onClick="addChar(this.form.display, '/')">
<br />
<input type="button" value=" 4 " onClick="addChar(this.form.display, '4')">
<input type="button" value=" 5 " onClick="addChar(this.form.display, '5')">
<input type="button" value=" 6 " onClick="addChar(this.form.display, '6')">
<input type="button" value=" * " onClick="addChar(this.form.display, '*')">
<br />
<input type="button" value=" 1 " onClick="addChar(this.form.display, '1')">
<input type="button" value=" 2 " onClick="addChar(this.form.display, '2')">
<input type="button" value=" 3 " onClick="addChar(this.form.display, '3')">
<input type="button" value=" - " onClick="addChar(this.form.display, '-')">
<br />
<input type="button" value=" 0 " onClick="addChar(this.form.display, '0')">
<input type="button" value=" . " onClick="addChar(this.form.display, '.')">
<input type="button" value="+/-" onClick="changeSign(this.form.display)">
<input type="button" value=" + " onClick="addChar(this.form.display, '+')">
<br />
<br />
<input type="button" value="Clear" onClick="this.form.display.value = 0 ">
<input type="button" value="Delete" onClick="deleteChar(this.form.display)">
<input type="button" value="=" name="enter" onClick="if (checkNum(this.form.display.value))
{ compute(this.form) }">
</form>
</body>
</html>

i have been going at it for about 10 minutes and pressed nearly every combination known and it seems to work fine.

one thing i did find is it doesn't work out 6--6=12.  it doesn't like two signs next to each other. dunno if that matters for most things though.

tyros8000 Wrote:
i have been going at it for about 10 minutes and pressed nearly every combination known and it seems to work fine.

one thing i did find is it doesn't work out 6--6=12.  it doesn't like two signs next to each other. dunno if that matters for most things though.


6--6 does actually equal 12 :p

cheers for your help Smile

i know it does.  but the calculator doesn't work that sort of sum with two signs next to eachother.
aaaah i see what you mean, hmm i will see if i can fix that, cheers
looks good after I gave it a spin
Reference URL's