Calculator JavaScript

This is a little example about a JavaScript Calculator, he could calculate some simple things another solution is to use eval.





JavaScript code:
var Wert = 0;
var OP = ":";
function CalcPlus ()
{
if(document.Eingabe.TB.value != ""&&OP!=":"){CalcErg();}
switch(OP)
{
case "+":
document.Eingabe.TB.value = 1*Wert+1*document.Eingabe.TB.value;document.Eingabe.TB.value="";OP=":";
break;
case "-":
document.Eingabe.TB.value = Wert-document.Eingabe.TB.value;document.Eingabe.TB.value="";OP=":";
break;
case "*":
document.Eingabe.TB.value = Wert*document.Eingabe.TB.value;document.Eingabe.TB.value="";OP=":";
break;
case "/":
document.Eingabe.TB.value = Wert/document.Eingabe.TB.value;document.Eingabe.TB.value="";OP=":";
break;
default:
Wert = document.Eingabe.TB.value;
document.Eingabe.TB.value="";
}
OP="+";
}
function CalcMinus ()
{
if(document.Eingabe.TB.value != ""&&OP!=":"){CalcErg();}
switch(OP)
{
case "+":
document.Eingabe.TB.value = 1*Wert+1*document.Eingabe.TB.value;document.Eingabe.TB.value="";OP=":";
break;
case "-":
document.Eingabe.TB.value = Wert-document.Eingabe.TB.value;document.Eingabe.TB.value="";OP=":";
break;
case "*":
document.Eingabe.TB.value = Wert*document.Eingabe.TB.value;document.Eingabe.TB.value="";OP=":";
break;
case "/":
document.Eingabe.TB.value = Wert/document.Eingabe.TB.value;document.Eingabe.TB.value="";OP=":";
break;
default:
Wert = document.Eingabe.TB.value;
document.Eingabe.TB.value="";
}
OP="-";
}
function CalcMal ()
{
if(document.Eingabe.TB.value != ""&&OP!=":"){CalcErg();}
switch(OP)
{
case "+":
document.Eingabe.TB.value = 1*Wert+1*document.Eingabe.TB.value;document.Eingabe.TB.value="";OP=":";
break;
case "-":
document.Eingabe.TB.value = Wert-document.Eingabe.TB.value;document.Eingabe.TB.value="";OP=":";
break;
case "*":
document.Eingabe.TB.value = Wert*document.Eingabe.TB.value;document.Eingabe.TB.value="";OP=":";
break;
case "/":
document.Eingabe.TB.value = Wert/document.Eingabe.TB.value;document.Eingabe.TB.value="";OP=":";
break;
default:
Wert = document.Eingabe.TB.value;
document.Eingabe.TB.value="";
}
OP="*";
}
function CalcDiv ()
{
if(document.Eingabe.TB.value != ""&&OP!=":"){CalcErg();}
switch(OP)
{
case "+":
document.Eingabe.TB.value = 1*Wert+1*document.Eingabe.TB.value;document.Eingabe.TB.value="";OP=":";
break;
case "-":
document.Eingabe.TB.value = Wert-document.Eingabe.TB.value;document.Eingabe.TB.value="";OP=":";
break;
case "*":
document.Eingabe.TB.value = Wert*document.Eingabe.TB.value;document.Eingabe.TB.value="";OP=":";
break;
case "/":
document.Eingabe.TB.value = Wert/document.Eingabe.TB.value;document.Eingabe.TB.value="";OP=":";
break;
default:
Wert = document.Eingabe.TB.value;
document.Eingabe.TB.value="";
}
OP="/";
}
function CalcErg ()
{
switch(OP)
{
case "+":
document.Eingabe.TB.value = 1*Wert+1*document.Eingabe.TB.value;
break;
case "-":
document.Eingabe.TB.value = Wert-document.Eingabe.TB.value;
break;
case "*":
document.Eingabe.TB.value = Wert*document.Eingabe.TB.value;
break;
case "/":
document.Eingabe.TB.value = Wert/document.Eingabe.TB.value;
break;
default:
document.Eingabe.TB.value=Wert;
}
OP=":";
Wert=document.Eingabe.TB.value;
}
function CalcClear ()
{
document.Eingabe.TB.value ="";OP=":";Wert=0;
}
  HTML code:
<form name="Eingabe">
<input type="text" name="TB">
<br>
<input type="button" onclick="javascript:CalcPlus()" value="+">
<input type="button" onclick="javascript:CalcMinus()" value="-">
<input type="button" onclick="javascript:CalcMal()" value="*">
<input type="button" onclick="javascript:CalcDiv()" value="/">
<input type="button" onclick="javascript:CalcErg()" value="=">
<br>
<input type="button" onclick="document.Eingabe.TB.value=Math.sqrt(document.Eingabe.TB.value)" value="sqrt">
<input type="button" onclick="document.Eingabe.TB.value=Math.sin(document.Eingabe.TB.value)" value="sin">
<input type="button" onclick="document.Eingabe.TB.value=Math.cos(document.Eingabe.TB.value)" value="cos">
<input type="button" onclick="document.Eingabe.TB.value=Math.tan(document.Eingabe.TB.value)" value="tan">
<br>
<input type="button" onclick="document.Eingabe.TB.value=1/document.Eingabe.TB.value" value="1/x">
<input type="button" onclick="document.Eingabe.TB.value=document.Eingabe.TB.value*1e3" value="x*1k">
<input type="button" onclick="document.Eingabe.TB.value=document.Eingabe.TB.value*1e-3" value="x*1m">
<input type="button" onclick="document.Eingabe.TB.value=Math.PI" value="Pi">
<br>
<input type="button" onclick="javascript:CalcClear()" value=" --< Clear >-- ">
<br>
</form>

2 thoughts on “Calculator JavaScript”

  1. I used to be suggested this web site via my cousin. I am no longer positive whether or not this publish is written by means of him as nobody else realize such designated about my difficulty.
    You’re incredible! Thanks!

    Feel free to surf to my web page;

Leave a Reply

Your email address will not be published.

one + fifteen =