Welcome to the third part of a six-part series on building a web page calculator. In the previous part I gave you the skeletons of our two standard calculator functions. But there are fine details to these functions that we have to examine and appreciate. We start with the showNum() function.
The explanation for the first two lines is as in the above cases. The next statement is an “if” statement. You cannot have more than one decimal point in a number. So the “if” condition uses regular expression techniques to check that. The line in the “if” statement writes the decimal point to the display.
The case to retrieve the number in memory (the store) is:
case "BRM":
document.getElementById('CI1').value = memVar;
break;
The only statement here simply copies the memory content to the display. In other words, the new display content becomes the content of the store.
The case to change the sign of the number of the display is:
case "BPM":
var temp = -1 * document.getElementById('CI1').value;
document.getElementById('CI1').value = temp;
break;
When the +/- button is clicked, the sign of the number on the display is changed. The first line for the case multiplies the value of the display by –1 and assigns the result to the temp local variable. The second line makes the value of temp the new content of the display.