next up previous contents index
Next: Showing Stimuli for a Up: Java and Java Script Previous: Validating forms (Java Script)   Contents   Index


A Calculator (Java-Script)

Here is a calculator in Java-Script which is a minor modification of a calculator by Axel zur Strassen.

Source:

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2//EN">
<HTML>
<HEAD>
   <TITLE></TITLE>
<SCRIPT language="JavaScript">

<!--
 function Ergebnis(FormIndex,ElemIndex)
 {
   var x = 0;
   x = eval(document.forms[FormIndex].elements[ElemIndex].value);
   document.forms[FormIndex].elements[ElemIndex].value = x;   
 }

 function Clear(FormIndex,ElemIndex)
 {
   document.forms[FormIndex].elements[ElemIndex].value = "";   
 }

 function Hinzufuegen(FormIndex,ElemIndex,Zeichen)
 {
   document.forms[FormIndex].elements[ElemIndex].value = 
   document.forms[FormIndex].elements[ElemIndex].value + Zeichen;   
 }

 function Sonderfunktion(FormIndex,ElemIndex,Funktion)
 {
   if(Funktion == "sqrt")
    {
     var x = 0;
     x = eval(document.forms[FormIndex].elements[ElemIndex].value);
     document.forms[FormIndex].elements[ElemIndex].value = Math.sqrt(x);   
        }
   if(Funktion == "pow")
    {
     var x = 0;
     x = eval(document.forms[FormIndex].elements[ElemIndex].value);
     document.forms[FormIndex].elements[ElemIndex].value = x * x;   
        }
   if(Funktion == "log")
    {
     var x = 0;
     x = eval(document.forms[FormIndex].elements[ElemIndex].value);
     document.forms[FormIndex].elements[ElemIndex].value = Math.log(x);   
        }
 }


//-->

</SCRIPT>
</HEAD>
<BODY>

<P><FORM method=post><!-- forms[0] --></P>

<CENTER><TABLE BORDER=1 CELLPADDING=10  BGCOLOR="#808080">
<TR>
<TD bgcolor=#C0C0C0><INPUT size=30 maxlength=30></TD>

</TR>

<TR>
<TD>
<TABLE>
<TR>
<TD width=50><INPUT type=button value="   7   " onClick="Hinzufuegen(0,0,'7')"></TD>
<TD width=50><INPUT type=button value="   8   " onClick="Hinzufuegen(0,0,'8')"></TD>
<TD width=70><INPUT type=button value="   9   " onClick="Hinzufuegen(0,0,'9')"></TD>
<TD width=50><INPUT type=button value="   +    " onClick="Hinzufuegen(0,0,'+')"></TD>
</TR>

<TR>
<TD width=50><INPUT type=button value="   4   " onClick="Hinzufuegen(0,0,'4')"></TD>
<TD width=50><INPUT type=button value="   5   " onClick="Hinzufuegen(0,0,'5')"></TD>
<TD width=70><INPUT type=button value="   6   " onClick="Hinzufuegen(0,0,'6')"></TD>
<TD width=50><INPUT type=button value="    -    " onClick="Hinzufuegen(0,0,'-')"></TD>
</TR>

<TR>
<TD width=50><INPUT type=button value="   1   " onClick="Hinzufuegen(0,0,'1')"></TD>
<TD width=50><INPUT type=button value="   2   " onClick="Hinzufuegen(0,0,'2')"></TD>
<TD width=70><INPUT type=button value="   3   " onClick="Hinzufuegen(0,0,'3')"></TD>
<TD width=50><INPUT type=button value="    *    " onClick="Hinzufuegen(0,0,'*')"></TD>
</TR>

<TR>
<TD width=100 colspan=2>
<INPUT type=button value="         =         " onClick="Ergebnis(0,0)"></TD>
<TD width=70><INPUT type=button value="   .    " onClick="Hinzufuegen(0,0,'.')"></TD>
<TD width=50><INPUT type=button value="    /    " onClick="Hinzufuegen(0,0,'/')"></TD>
</TR>

<TR>
<TD width=50><INPUT type=button value="sqrt " 
    onClick="Sonderfunktion(0,0,'sqrt')"></TD>
<TD width=50><INPUT type=button value=" pow "
    onClick="Sonderfunktion(0,0,'pow')"></TD>
<TD width=70><INPUT type=button value=" log "
    onClick="Sonderfunktion(0,0,'log')"></TD>
<TD width=70><INPUT type=button value=" C " onClick="Clear(0,0)"></TD>
</TR>
</TABLE>
</TD>
</TR>
</TABLE></CENTER>

<P></FORM>
<HR WIDTH="100%"></P>

</BODY>
</HTML>


next up previous contents index
Next: Showing Stimuli for a Up: Java and Java Script Previous: Validating forms (Java Script)   Contents   Index
Oliver Kirchkamp