Converting Values

The possibilities are endless for the types of values that can be converted from one form to another. Script 17.10 shows just one example: how to convert kilometers to miles.

To convert kilometers to miles:

1.
var t,expr=prompt('Length in kilometers:','');
The bookmarklet starts by prompting the user for a length in kilometers (Figure 17.23).
Figure 17.23. First, ask for the number to be converted.

2.
if(isNaN(parseFloat(expr)))
Check to see if the user entered a numeric value.
3.
{t=expr+' is not a number';}
If not, set t to be an error message.
4.
else{t='Length in miles is '+Math.round(expr*6214)/10000;}
Otherwise, convert ...

Get JavaScript and Ajax for the Web: Visual QuickStart Guide, Seventh Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.