Web server type - 1359Chapter 53 .Application: Calculations and Graphics } else

January 19th, 2008

1359Chapter 53 .Application: Calculations and Graphics } else { return + ohmage + ohms } } } The selections from the pop-up menus meet the calculation formulas of resistors in the calcOhms() function. Because this function is triggered indirectly by each of the SELECT objects, sending any of their parameters to the function is a waste of effort. Moreover, the calcOhms() function is invoked by the onLoadevent handler, which is not tied to the form or its controls. Therefore, the function obtains the reference to the form and then extracts the necessary values of the four SELECT objects by using explicit (named) references. Each value is stored in a local variable for convenience in completing the ensuing calculation. Recalling the rules used to calculate values of the resistor bands, the first statement of the calculation multiplies the tens pop-up value times 10 to determine the tens digit and then adds the ones digit. From there, the combined value is multiplied by the exponent value of the selected multiplier value. Notice that the expression first assembles the value as a string to concatenate the exponent factor and then evaluates it to a number. Although I try to avoid the eval()function because of its slow performance, the one call per calculation is not a performance issue at all. The evaluated number is passed to the format() function for proper formatting (and setting of order of magnitude). In the meantime, the tolerance value is extracted from its array, and the combined string is plugged into the result text field (which is in a separate form, as described later): // calculate resistance and tolerance values function calcOhms() { var form = document.forms[ rescalc ] var d1 = form.tensSelect.selectedIndex var d2 = form.onesSelect.selectedIndex var m = form.multiplierSelect.selectedIndex var t = form.toleranceSelect.selectedIndex var ohmage = (d1 * 10) + d2 ohmage = eval( + ohmage + e + multiplier[m]) ohmage = format(ohmage) var tol = tolerance[t] document.forms[ ouput ].result.value = ohmage + , + tol } Preloading images As part of the script that runs when the document loads, the next group of statements precaches all possible images that can be displayed for the resistor art. For added scripting convenience, the color names are loaded into an array. With the help of that just-created array of color names, you then create another array (imageDB), which both generates Image objects for each image file and assigns a URL to each image. Notice an important subtlety about the index values being used to create each entry of the imageDB array: Each index is a colorArray entry, which is the name of the color. As you found out in Chapter 37, if you create an array
You want to have a cheap webhost for your apache application, then check apache web hosting services.

1358 Part V . Putting (Web hosting account) JavaScript to Work

January 19th, 2008

1358 Part V . Putting JavaScript to Work Basic arrays In calculating the resistance, the script needs to know the multiplier value for each color. If not for the last two multiplier values actually being negative multipliers (for example, 10-1 and 10-2), I could have used the index values without having to create this array. But the two out-of-sequence values at the end make it easier to work with an array rather than to try special-casing these instances in later calculations: // create array listing all the multiplier values var multiplier = new Array() multiplier[0] = 0 multiplier[1] = 1 multiplier[2] = 2 multiplier[3] = 3 multiplier[4] = 4 multiplier[5] = 5 multiplier[6] = 6 multiplier[7] = 7 multiplier[8] = 8 multiplier[9] = 9 multiplier[10] = -1 multiplier[11] = -2 // create object listing all tolerance values var tolerance = new Array() tolerance[0] = +/-5% tolerance[1] = +/-10% tolerance[2] = +/-20% Although the script doesn t do any calculations with the tolerance percentages, it needs to have the strings corresponding to each color for display in the pop-up menu. The tolerance array is there for that purpose. Calculations and formatting Before the script displays the resistance value, it needs to format the numbers in values that are meaningful to those who know about these values. Just as measures of computer storage bytes, high quantities of ohms are preceded with kilo and meg prefixes, commonly abbreviated with the K and M letters. The format() function determines the order of magnitude of the final calculation (from another function shown in the following section) and formats the results with the proper unit of measure: // format large values into kilo and meg function format(ohmage) { if (ohmage >= 1e6) { ohmage /= 1e6 return + ohmage + Mohms } else { if (ohmage >= 1e3) { ohmage /= 1e3 return + ohmage + Kohms
Searching for affordable and reliable webhost to host and run your web applications? Go to our java web server services and you will be pleased.

1357Chapter 53 .Application: Calculations and Graphics Figure 53-1: (Web space)

January 19th, 2008

1357Chapter 53 .Application: Calculations and Graphics Figure 53-1: The Resistor Calculator with images inside a table border The Code All the action takes place in the file named resistor.htm. A second document is an introductory HTML text document that explains what a resistor is and why you need a calculator to determine a component s value. The article, called The Path of Least Resistance, can be viewed in a secondary window from a link in the main win dow. Here you will be looking only at resistor.htm, which has been updated to include style sheets. The document begins in the traditional way. It specifies a JavaScript 1.1-level language because you will be using several features of that language version: Graphical Resistance Calculator