Welcome to the second part of a six-part series on how to build a calculator for your web page. In the previous part, we got into the basics of setting up the calculator's layout and what functions we want it to have. In this part, we will begin to code the functions.
You can either press a button by clicking it or by using the keyboard. Pressing by clicking requires you to give each button the onclick attribute. I included the onclick attribute for each button in the previous part of this series. I will address using the keyboard later in the series.
In this part of the series, a button is pressed by clicking. When a button is clicked, the onclick event is triggered (calls a function). We shall look at the functions in this part of the series.
Standard Calculator User Requirements
In this section I explain the requirements from the user's point of view.
When the calculator is opened, the initial value in the display is zero.
As soon as you type a digit, the digit typed replaces the zero.
When a number appears on the display, clicking a digit joins the number on the display to the right.
When you click the +, -, X or / key, the next number typed replaces the number on the display. This should result in the +, -, X, or / operation.
When the equal sign button is clicked, the result replaces the number (group of digits) on the display.
When the M+ (store in memory) button is clicked, the number on the display is stored in memory. After this, clicking another digit replaces the number on the display.
You can erase the display by pressing a button.
When the equal sign is clicked, the operation (+, - X or /) combining the previous number on the display and the present number is evaluated. The result replaces the present number on the display.