If somebody ever asked me, "what's the dorkiest piece of code you've ever written," this calculator would be near the top. I can't believe I even spent an hour on this (90 percent of which was making those cute little buttons).
var displayvalue = ""; // what is shown in the display window of the calculator
var equation = ""; // the running equation to be calculated
var newdisplay = true; // we're done with the value in the window and are ready for a new number
// sets the value displayed on the calculator
function setDisplayValue(newval) {
// if we're done with the screen value, we'll overwrite it with a blank
if (newdisplay) {
displayvalue="";
}
// concatenate the new number onto the display value
displayvalue += newval;
document.calculator.currval.value = displayvalue;
// so far, we're pretty sure we will want the next entry to be concatenated
newdisplay = false;
}
// evaluate the equation
function equals() {
// add the current displayvalue to the string
equation += displayvalue;
// evaluate & display
displayvalue = eval (equation);
document.calculator.currval.value = displayvalue;
// reset
equation = "";
newdisplay = true;
}
// concatenate a value and an operator onto the equation
function operator(opr) {
// concatenate the value
equation += displayvalue;
// display the current running total
displayvalue = eval(equation);
document.calculator.currval.value = displayvalue;
// concatenate the operator
equation += opr;
// reset the newdisplay value
newdisplay = true;
}