In this fourth part of a six-part series on building a web page calculator, we'll delve more deeply into the functions. We start with the operator(ID) function, and then take a closer look at events. We'll wrap up by starting to show how to use the keyboard with the calculator.
When you click any of the operator buttons (+, -, X, or /) or the M+, C or equal sign buttons, the operator() function is called. When the function is called, the above segment reads the number that is on the display. This number forms the first part of the string for the evaluation. It assigns the number to the evaluation string.
Remember that the arithmetic expression is composed into a string assigned to the evalString variable. This variable will be the argument for the top level JavaScript eval() function which will give us the result of the arithmetic expression. The initial value of evalString is "".
You may be wondering why I have used the shorthand assignment operator +=. Assume that you want to compute "2 + 3" and you have typed "2+". When you type 3 and press the equal sign, the above segment will append the character 3 to "2+", giving you "2+3".