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.
This is not a case for the switch statement. From the explanation of all the code I have given above, we see that there can be one leading zero for the number on the display when there is no decimal point. I decided that any such leading zero should be removed. This is the behavior of physical calculators.
This code segment removes the leading zero. It first of all copies the content of the display to the strToTest local variable. It then uses a regular expression to see if there is a leading zero. If there is, it removes it from the value of the strToTest variable. Then it copies the result back to the display.
We have done enough for this part of the series. We continue in the next part.
DISCLAIMER: The content provided in this article is not warranted or guaranteed by Developer Shed, Inc. The content provided is intended for entertainment and/or educational purposes in order to introduce to the reader key ideas, concepts, and/or product reviews. As such it is incumbent upon the reader to employ real-world tactics for security and implementation of best practices. We are not liable for any negative consequences that may result from implementing any information covered in our articles or tutorials. If this is a hardware review, it is not recommended to open and/or modify your hardware.