Created
August 21, 2013 03:45
-
-
Save DerekRies/6290137 to your computer and use it in GitHub Desktop.
The worst code I've ever seen. http://www.anaesthetist.com/mnm/javascript/calc.htm
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!doctype html> | |
<html> | |
<head> | |
<meta charset="UTF-8"> | |
<title>Untitled Document</title> | |
<script type="text/javascript"> | |
<!-------------------------------------------------------------------- | |
Memory = "0"; // initialise memory variable | |
Current = "0"; // and value of Display ("current" value) | |
Operation = 0; // Records code for eg * / etc. | |
MAXLENGTH = 30; // maximum number of digits before decimal! | |
function AddDigit(dig) //ADD A DIGIT TO DISPLAY (keep as 'Current') | |
{ if (Current.indexOf("!") == -1) //if not already an error | |
{ if ( (eval(Current) == 0) | |
&& (Current.indexOf(".") == -1) | |
) { Current = dig; | |
} else | |
{ Current = Current + dig; | |
}; | |
Current = Current.toLowerCase(); //FORCE LOWER CASE | |
} else | |
{ Current = "Hint! Press 'AC'"; //Help out, if error present. | |
}; | |
if (Current.indexOf("e0") != -1) | |
{ var epos = Current.indexOf("e"); | |
Current = Current.substring(0,epos+1) + Current.substring(epos+2); | |
}; | |
if (Current.length > MAXLENGTH) | |
{ Current = "Aargh! Too long"; //don't allow over MAXLENGTH digits before "." ??? | |
}; | |
document.Calculator.Display.value = Current; | |
} | |
function Dot() //PUT IN "." if appropriate. | |
{ | |
if ( Current.length == 0) //no leading ".", use "0." | |
{ Current = "0."; | |
} else | |
{ if ( ( Current.indexOf(".") == -1) | |
&&( Current.indexOf("e") == -1) | |
) | |
{ Current = Current + "."; | |
}; }; | |
document.Calculator.Display.value = Current; | |
} | |
function DoExponent() | |
{ | |
if ( Current.indexOf("e") == -1 ) | |
{ Current = Current + "e0"; | |
document.Calculator.Display.value = Current; | |
}; | |
} | |
function PlusMinus() | |
{ | |
if (Current.indexOf("e") != -1) | |
{ var epos = Current.indexOf("e-"); | |
if (epos != -1) | |
{ Current = Current.substring(0,1+epos) + Current.substring(2+epos); //clip out -ve exponent | |
} else | |
{ epos = Current.indexOf("e"); | |
Current = Current.substring(0,1+epos) + "-" + Current.substring(1+epos); //insert -ve exponent | |
}; | |
} else | |
{ if ( Current.indexOf("-") == 0 ) | |
{ Current = Current.substring(1); | |
} else | |
{ Current = "-" + Current; | |
}; | |
if ( (eval(Current) == 0) | |
&& (Current.indexOf(".") == -1 ) | |
) { Current = "0"; }; | |
}; | |
document.Calculator.Display.value = Current; | |
} | |
function Clear() //CLEAR ENTRY | |
{ Current = "0"; | |
document.Calculator.Display.value = Current; | |
} | |
function AllClear() //Clear ALL entries! | |
{ Current = "0"; | |
Operation = 0; //clear operation | |
Memory = "0"; //clear memory | |
document.Calculator.Display.value = Current; | |
} | |
function Operate(op) //STORE OPERATION e.g. + * / etc. | |
{ | |
if (Operation != 0) { Calculate(); }; //'Press "=" if pending operation! | |
// note that design is not good for showing *intermediate* results. | |
if (op.indexOf("*") > -1) { Operation = 1; }; //codes for * | |
if (op.indexOf("/") > -1) { Operation = 2; }; // slash (divide) | |
if (op.indexOf("+") > -1) { Operation = 3; }; // sum | |
if (op.indexOf("-") > -1) { Operation = 4; }; // difference | |
Memory = Current; //store value | |
// note how e.g. Current.value gives neither error nor value! *** | |
Current = ""; | |
document.Calculator.Display.value = Current; | |
} | |
function Calculate() //PERFORM CALCULATION (= button) | |
{ | |
if (Operation == 1) { Current = eval(Memory) * eval(Current); }; | |
if (Operation == 2) | |
{ if (eval(Current) != 0) | |
{ Current = eval(Memory) / eval(Current) | |
} else | |
{ Current = "Aargh! Divide by zero"; //don't allow over MAXLENGTH digits before "." ??? | |
} | |
}; | |
if (Operation == 3) { Current = eval(Memory) + eval(Current); }; | |
if (Operation == 4) { Current = eval(Memory) - eval(Current); }; | |
Operation = 0; //clear operation | |
Memory = "0"; //clear memory | |
Current = Current + ""; //FORCE A STRING! | |
if (Current.indexOf("Infinity") != -1) //eg "1e320" * 1 | |
{ Current = "Aargh! Value too big"; | |
}; | |
if (Current.indexOf("NaN") != -1) //eg "1e320" / "1e320" | |
{ Current = "Aargh! I don't understand"; | |
}; | |
document.Calculator.Display.value = Current; | |
// NOTE: if no operation, nothing changes, Current is left the same! | |
} | |
function FixCurrent() | |
{ | |
Current = document.Calculator.Display.value; | |
Current = "" + parseFloat(Current); | |
if (Current.indexOf("NaN") != -1) | |
{ | |
Current = "Aargh! I don't understand"; | |
}; | |
document.Calculator.Display.value = Current; | |
} | |
//---------------------------------------------------------------> | |
</script> | |
</head> | |
<body> | |
*From <a href="http://www.anaesthetist.com/mnm/javascript/calc.htm">http://www.anaesthetist.com/mnm/javascript/calc.htm</a> | |
<FORM name="Calculator"> | |
<table width="45%" border="4" bgcolor="#9999CC"><tr> <!--OUTER MARGIN OF CALCULATOR--> | |
<td colspan="2" align="center"> | |
<b><font face="Verdana, Arial, Helvetica" color="#000080" size="3"> | |
A Simple JavaScript Calculator<br></font> | |
<font face="Courier" size="5"> | |
<input type="text" maxlength="40" size="25" name="Display" onChange="FixCurrent()"> | |
</font></b> | |
</td></tr> | |
<tr><td width="65%" align="center"> <!--left panel------> | |
<table><tr> | |
<td><input type="button" name="seven" value=" 7 " OnClick="AddDigit('7')"></td> | |
<td><input type="button" name="eight" value=" 8 " OnClick="AddDigit('8')"></td> | |
<td><input type="button" name="nine" value=" 9 " OnClick="AddDigit('9')"></td> | |
</tr><tr> | |
<td><input type="button" name="four" value=" 4 " OnClick="AddDigit('4')"></td> | |
<td><input type="button" name="five" value=" 5 " OnClick="AddDigit('5')"></td> | |
<td><input type="button" name="six" value=" 6 " OnClick="AddDigit('6')"></td> | |
</tr><tr> | |
<td><input type="button" name="one" value=" 1 " OnClick="AddDigit('1')"></td> | |
<td><input type="button" name="two" value=" 2 " OnClick="AddDigit('2')"></td> | |
<td><input type="button" name="three" value=" 3 " OnClick="AddDigit('3')"></td> | |
</tr><tr> | |
<td><input type="button" name="plusmin" value=" +/- " OnClick="PlusMinus()"></td> | |
<td><input type="button" name="one" value=" 0 " OnClick="AddDigit('0')"></td> | |
<td><input type="button" name="two" value=" . " OnClick="Dot()"></td> | |
</tr> | |
</table> | |
</td> <!--end left panel--> | |
<td width="35%" align="center"> <!--right panel-----> | |
<table><tr> | |
<td><input type="button" name="clear" value=" C " OnClick="Clear()"></td> | |
<td><input type="button" name="AC" value=" AC " OnClick="AllClear()"></td> | |
</tr><tr> | |
<td><input type="button" name="mul" value=" * " OnClick="Operate('*')"></td> | |
<td><input type="button" name="div" value=" / " OnClick="Operate('/')"></td> | |
</tr><tr> | |
<td><input type="button" name="add" value=" + " OnClick="Operate('+')"></td> | |
<td><input type="button" name="sub" value=" - " OnClick="Operate('-')"></td> | |
</tr><tr> | |
<td><input type="button" name="result" value=" = " OnClick="Calculate()"></td> | |
<td align="right"><input type="button" name="exp" value=" EXP " OnClick="DoExponent()"></td> | |
</tr></table> | |
</td> <!--end right panel--> | |
</tr></table> <!--END OUTER MARGIN CALC-------> | |
</FORM></div> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Best JS code ever!