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

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.

Leave a Reply