A Bookmarklet Calculator

If you think about it, it’s really a bit too difficult to do a full-fledged calculator with buttons and a running value on just one long line of code. However, you can use a bookmarklet like Script 17.11 to do fairly complex calculations, using JavaScript’s built-in Math functions, as described in Table 17.1.

Table 17.1. JavaScript’s Math Functionality
FunctionDescription
absAbsolute value
sin, cos, tanStandard trigonometric functions; arguments in radians
acos, asin, atanInverse trigonometric functions; return values in radians
exp, logExponential and natural logarithm, base e
ceilReturns least integer greater than or equal to argument
floorReturns greatest integer less than or equal to argument
minReturns lesser of two arguments
maxReturns greater of two arguments
powExponential; first argument is base, second is exponent
roundRounds argument to nearest integer
sqrtSquare root

To use a JavaScript calculator:

1.
var evl,expr=prompt('Formula... (eg: 2*3 +7/8)',''),



This line sets up a variable evl, and then prompts the user for an expression or formula, as shown in Figure 17.25, which is stored in expr.

Figure 17.25. The user must be prompted for a formula.


2.
with(Math)try{



The next few lines need to be evaluated using JavaScript’s built-in Math routines. The with(Math) part tells the interpreter that when any of these functions are seen, to evaluate them as Math commands.

The try{} warns JavaScript that what we’re doing may well fail, and if so, don’t panic. In fact, don’t even put up an error message if there’s a problem, as we’ll be handling the errors ourselves.

3.
evl=parseFloat(eval(expr));



Evaluate the expression, and turn it into a floating-point number, which is then stored in evl.

4.
if(isNaN(evl))



If the resulting value in evl is not a number (NaN), do the following line.

Script 17.11. Surprisingly complex equations can be evaluated with this bookmarklet.
javascript:(function(){var evl,expr=prompt('Formula...(eg: 2*3 + 7/8)',''),with(Math)try{evl=parseFloat(eval(expr));if(isNaN(evl)){throw Error('Not a number!'),}prompt('Result of '+expr+':',evl);}catch(evl){alert(evl);}})();

5.
{throw Error('Not a number!'),}



If we’re here, for some reason what the user entered didn’t work out to a number. When that happens we want to force an error message of “Not a number!” to display. Here’s where the message is set; it will be displayed in step 7.

6.
prompt('Result of '+expr+':',evl);



Otherwise, the expression was valid, so display the result, as shown in Figure 17.26.

Figure 17.26. JavaScript returns the result of the calculation.


7.
}catch(evl){alert(evl);}



Here’s the end of that try{} block that started in step 2. To get here, one of two things happened: either we ran into the error in step 5, or some other error entirely occurred. Either way, we “catch” the error that was “thrown” and put it up on the screen in an alert.

✓ Tip

  • Trying to remember where you’ve seen that try/throw/catch syntax before? It was originally covered back in the “Handling Errors” section of Chapter 2.


..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.145.66.241