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.
Coding a Web Page Calculator - The operator() Function (Page 4 of 5 )
The other function, operator(), carries out operations. It has to carry out the ADD, SUBTRACT, MULTIPLY and DIVIDE operations. It also has to store, clear the display and do evaluation (Enter). This is its function skeleton with its associated global variables:
var evalString = ""
var memVar = "";
var toClearDisplay = false;
function operator(ID)
{
//first construct the first part of the eval argument using JavaScrips top level eval() function
switch (ID)
{
case "BP":
//join and + and to the evaluation string, evalString.
break;
case "BMI":
//join and - and to the evaluation string, evalString.
break;
case "BX":
//join and * and to the evaluation string, evalString.
break;
case "BD":
//join and / and to the evaluation string, evalString.
break;
case "BMP":
// Store display content into memory
break;
case "BMM":
//Clear the display and do other associated things.