| ← C6: Algorithms & Programming | ↑ Table of Contents ↑ | C7: Computer Science as a Discipline → |
To me, mathematics, computer science, and the arts are insanely related. They're all creative expressions. — Sebastian Thrun
Randomness has an incredibly powerful place in our culture. If you think about it, you can see it driving the algorithms that run our information economy, patterns that make up the traffic of our cities, and on over to the way the stars and galaxies formed. — Paul D. Miller (aka DJ Spooky)
Chapter X5 introduced JavaScript features that enabled the development of fully interactive Web pages. You were able to enter text into the page via text boxes, then initiate events (e.g., by clicking on a button) that would access that text and integrate it into the page. For example, the invitation generator page (EXAMPLE 5.5) enabled you to customize an invitation by entering input values (recipient name, event description, and date) and those values would be incorporated into a custom invitation at the click of a button. While many Web-based applications involve inputting and processing text, there are many more that instead process numbers. For example, a page that calculates how much you should tip for a meal would require you to enter the check amount and the desired percentage. From those values, it could calculate the tip amount and display it in the page.
In this chapter, you will extend your capabilities by using numbers and mathematical operations to create Web pages that calculate. The number data type will be introduced, as well as some of JavaScript's predefined mathematical functions that can be used for more complex calculations.
Each unit of information processed by a computer belongs to a general category, or data type, which defines the ways in which the information can be handled. In JavaScript, text values (sequences of characters enclosed in single quotes) are referred to as strings. In addition to the string data type, JavaScript values can also be numbers and Booleans (true/false values, which will be explored in a later chapter). Each JavaScript data type is associated with a specific set of predefined operators, which are part of the JavaScript language and may be used by programmers to manipulate values of that type. For example, we have already seen that strings can be glued (or concatenated) together using the + operator. Similarly, JavaScript predefines the standard arithmetic operators + (addition), - (subtraction), * (multiplication), / (division), and ** (exponentiation) for use with numbers.
Mathematical operators can be applied to numbers as well as variables that store numbers. FIGURE 1 below shows several examples of assignments to variables using math operators.
x = 7 * 2; | (7 * 2) = 14 is assigned to x |
y = x + 1; | (14 + 1) = 15 is assigned to y |
z = (y - x) / 2; | (15 - 14) / 2 = 0.5 is assigned to z |
z = z + 1; | (0.5 + 1) = 1.5 is reassigned to z |
w = 2**10; | 210 = 1024 is assigned to w |
FIGURE 1. Examples of assignment involving math operators.
In mathematics, the symbol = is used to indicate equality. Thus, a statement such as z = z + 1; wouldn't make much sense in an algebraic context. However, you must remind yourself that JavaScript uses = to assign values to variables, rather than to determine equality. The expression on the right-hand side of an assignment is first evaluated using the current values of any variables. Then, the value of that expression is assigned to the variable on the left-hand side of the assignment. In the example z = z + 1; the result is that the variable z has its value increased by 1. It may help to think of = as the gets operator. When you read the statement z = z + 1; think if it as saying that "z gets z + 1." This reading emphasizes the directionality of an assignment: the variable on the left-hand side is being assigned the value of the expression on the right-hand side.
When working with JavaScript numbers, it may help to know the following:
Designer Secrets
According to mathematical convention, multiplication and division have "higher precedence" in expressions than addition and subtraction do. This means that, in an expression containing both a multiplication/division and an addition/subtraction operation, the multiplication/division operator is applied first. Thus, the expression (2+4*5) will evaluate to (2+(4*5)) = 22. If more than one operator of the same precedence appears in an expression, such as a series of divisions, the operators are applied from left to right. Thus, (8/4/2) will evaluate to ((8/4)/2) = 1.
Although JavaScript adheres strictly to these precedence rules, it can be risky to rely on the rules when creating complex mathematical expressions. For example, a programmer might write (x+1 * z), intending that the value of x be incremented before the multiplication occurs. However, the browser would ignore the spacing and perform the higher precedence multiplication first. To avoid this kind of mistake, use parentheses to group subexpressions. This will make your expressions easier to read and will ensure that the operators are evaluated in the desired order. Using this technique, the expression above would be written as ((x+1) * z).
EXERCISE 6.1: To what number value does each of the following JavaScript expressions evaluate?
1 + (2 * 3) (1 + 2) * 3 9 / 2 4**2 5e2 + 3
In Chapter X5, you used text boxes to read and process characters typed by you. There is nothing to stop you from entering a number into a text box, accessing that box's contents and incorporating it into the page (as before). However, a text box is not ideal for inputting numbers for several reasons. First, there is no error checking with a text box. If you intend to enter a number but add an extraneous character (e.g. 1q2 when you meant 12), the browser will let you. However, an error will occur when you try to apply a mathematical operator to something that is clearly not a number. Second, even if you enter a number in a text box, it will be treated as a string when accessed using the value attribute. This can lead to subtle errors when performing mathematical operations.
Fortunately, HTML provides another type of input element that is specifically designed for entering numbers. A number box is like a text box, except that its type attribute is assigned to be "number" instead of "text":
<input type="number" id="ELEMENT_ID">
Recall that text boxes had optional attributes that could be used to initialize it contents and resize the box. The value attribute works the same for number boxes — if you assign a number value to the value attribute, that number will appear in the box when the page is loaded. The size attribute, however, does not work to resize number boxes. The equivalent effect can be obtained by assigning the min and max attributes, with their values designating the intended smallest and largest values for that box. If you assign numbers to min and max, the number box will appear wide enough to fit that range of numbers. For example, the following number box would initially contain the number 1024 and would be wide enough to fit 9 digits.
<input type="number" id="numBox" min=0 max=999999999" value=1024>
Number boxes have a new attribute, valueAsNumber, which is used to access the number in the box. For example, if you entered 10 in a number box named numBox, then numBox.valueAsNumber would evaluate to the number 10 (not the string '10').
The HTML document in EXAMPLE 6.1 contains a number box, with id id="numBox". In the DoubleIt function, that number entered in the box is accessed using numBox.valueAsNumber, and its double is displayed.
EXERCISE 6.2: Confirm that the Web page from EXAMPLE 6.1 behaves as described by clicking the "See in Browser" button and clicking on the button. Enter different numbers in the number box and confirm that each is doubled correctly
While a number box also has a value attribute, you should avoid using it when accessing the contents since value always evaluates to a string. For example, if you entered 10 in a number box named numBox, then numBox.value would evaluate to the string '10' (not the number 10).
EXERCISE 6.3: Modify the first JavaScript statement in the DoubleIt function from EXAMPLE 6.1 so that it accesses the value attribute instead of the valueAsNumber attribute. This will cause an unexpected result, as the num variable will be assigned a string value instead of a number. Applying the '+' operator to strings glues them together instead of adding. Confirm this behavior.
The HTML document in EXAMPLE 6.2 serves as a simple tip calculator. The resulting page contains two number boxes in which you can enter the check amount and the tip percentage. The boxes are wide enough to accommodate the range of values specified by the min and max attributes (7 characters wide for amountBox and 3 characters wide for percentBox). Since amountBox does not have a value attribute, it will initially be empty. The percentBox will contain the default value 20. When the button is clicked, the Calculate function is called to calculate and display the tip amount using the numbers from the boxes. Note that the tip amount will display using as many decimal places as needed. This looks odd, since dollar amounts are typically rounded to two decimal places, denoting cents. You will learn how to adjust this shortly.
EXERCISE 6.4: Confirm that the Web page from EXAMPLE 6.2 behaves as described by clicking the "See in Browser" button, entering an amount in the box, and clicking the button. Determine the 20% tip for each of the following amounts: $10.00 $20.00 $15.99 $63.39
Similarly, determine what a 22% tip would be on each of the above check amounts?
Chapter X5 introduced variables as names that store values. By assigning the contents of a text box to a variable, that succinct variable name could be used in complex expressions which simplified the code. For example, in EXAMPLE 6.2, the variables amount and percent were assigned to store the numbers from the text boxes, which simplified the display message. In addition to serving as shortcuts for text or number box contents, variables are also useful for temporarily storing the results of computations. For example, we could use a variable to store the result of the tip calculation, then use that variable to simplify the output message even further. This is done in EXAMPLE 6.3.
Another advantage of using variables to store the results of a computation is that it simplifies formatting that result when displaying it. The suffix .toFixed(#) can be added to a variable to round the number to a fixed number of decimal places. The desired number of decimal places is specified in the parentheses (in place of #). In EXAMPLE 6.3, the variable tip is formatted so that the number is rounded to two decimal places: tip.toFixed(2). This has the effect of displaying the number in a dollar and cents format.
EXERCISE 6.5: Confirm that the Web page from EXAMPLE 6.3 behaves as described by clicking the "See in Browser" button and clicking on the button. As before, determine the rounded 20% tip for each of the following amounts: $10.00 $20.00 $15.99 $63.39
What would you expect to happen if you changed the rounding number from 2 to 0, i.e., tip.toFixed(0). Make this change in EXAMPLE 6.3 and confirm the outcome.
Suppose you have decided to invest in your future. You have a certain amount of money to invest, say $1000.00, and a long-term investment account that would pay yearly interest, say 3.5%. The first year of your investment, you would earn $35.00 in interest, giving you a total of $1035.00 in the account. In the second year, you would earn 3.5% interest on this new total, giving you $36.22 in interest for a total of $1071.22. After 10 years, your $1000 investment will grow to $1410.60.
The formula for calculating the compound interest on an investment is as follows:

where amount is the amount to be invested, rate is the yearly interest rate, and years is the number of years for the investment. The HTML document in EXAMPLE 6.4 implements this computation. The resulting Web page has three number boxes in which you can enter the amount, rate and years. At the click of the button, the numbers are accessed and assigned to variables, and those variables are used in computing the final amount after accumulating compound interest. Note that the ** operator is used to raise a value to a power, and the .toFixed(2) suffix is used to round the amounts to two decimal places.
EXERCISE 6.6: Confirm that the Web page from EXAMPLE 6.4 behaves as described by clicking the "See in Browser" button and clicking on the button. What would be the final amount if you invested $1000 dollars at 3.5% for 20 years? What would be the final amount if you invested $1000 dollars at 4.5% for 20 years?
EXERCISE 6.7: To determine how long it will take to double an initial investment, simply divide 72 by the interest rate. For example, if you have a 3.6% interest rate, it will require 72/3.6 = 20 years. Confirm that this rule works using the Web page in EXAMPLE 6.4.
Next, modify the HTML document in FIGURE 6.4 to instead calculate the number of years required to double the initial investment. Since the number of years is to be calculated, the number box for years should be removed. Likewise, the statement that previously accessed the years from the box needs to be replaced with one that calculates the number of years required. Finally, the message should be changed to display the number of years and the final amount, e.g., "In 20 years, your investment will double to $2028.59."
Designer Secrets
Since blank lines are ignored by the browser, they have no impact on the behavior of the page. However, they can make the code easier for developers to read. In the same way that paragraphs in an essay break the content into related groups of sentences, blank lines can be used to break a series of JavaScript statements into related groups. In the ShowInterest function (EXAMPLE 6.4), there are three statements whose purpose is to access the contents of number boxes. Then, there is a statement that performs a computation, followed by a statement that displays a message in the page. Inserting blank lines effectively highlights the three tasks being performed by the function.
This same pattern applies to the Calculate function in EXAMPLES 6.2 and 6.3. You will find that many of the interactive Web pages you will write fall into this general pattern: a function, which is called at the click of a button, must (1) access the contents of number boxes to receive user input, (2) perform one or more computations, and (3) display the results of those computations. Inserting blank lines between the statements that perform these tasks will help organize the functions you write and make them easier to understand afterwards.
In mathematical terms, a function is a mapping from some number of inputs to a single output. The math operators we have previously seen, e.g., +, -, *, / and **, are symbols that denote functions. For example, the + operator takes two inputs and calculates their sum (1 + 2 → 3). Similarly, the ** operator takes two numbers and calculates the first raised to the power of the second (2**3 → 23 → 8).
In addition to these built-in operators, JavaScript provides an extensive library of predefined mathematical functions (FIGURE 2). All these functions begin with the prefix Math., which signifies that function is part of a library of mathematical functions.
| Function | Input(s) | Description | Example |
|---|---|---|---|
Math.abs | one number | Absolute value | Math.abs(-4) → 4 |
Math.sqrt | one number | Square root | Math.sqrt(81) → 9 |
Math.floor | one number | Floor (rounds down) | Math.floor(1.3) → 1 |
Math.ceil | one number | Ceiling (rounds up) | Math.ceil(1.3) → 2 |
Math.round | one number | Round (to nearest integer) | Math.round(1.3) → 1 |
Math.max | two numbers | Maximum | Math.max(3.6, 2.1) → 3.6 |
Math.min | two numbers | Minimum | Math.min(3.6, 2.1) → 2.1 |
FIGURE 2. Predefined math functions in JavaScript.
From a programmer's point of view, a function can be viewed as a unit of computational abstraction. When you call the Math.sqrt function in the following JavaScript statement:
root = Math.sqrt(12.25);
you do not need to know the computational steps involved in performing the calculation of the square root. You simply apply the predefined Math.sqrt function and it produces the desired result (Math.sqrt(12.25) → 3.5). With this perspective in mind, we will often refer to applying a function to inputs as calling that function. We will also refer to the output of the function as the return value for the function call.
FIGURE 3 shows an interactive visualizer, where you can select the math function, enter number(s) in the box(es), and see the result of that function call. For example, the first default is a call to the Math.sqrt function with input 1024. When you click on the button to the right, the value returned by that call (Math.sqrt(1024) → 32) is displayed below. You can enter different inputs in the number box or even select a different function using the pull-down menu. Likewise, the second default displays the result of the function call Math.max(-1.2, 2) → 2.
Select the function you want to test, enter values for the input(s),
then click the button to the right to see the result.
| () | |
| (, ) | |
FIGURE 3. Interactive function visualizer.
EXERCISE 6.8: Predict what each of the following function calls would return, then use the visualizer in FIGURE 3 to confirm your answers.
Math.abs(-33) Math.ceil(2.8) Math.floor(2.8) Math.round(2.8) Math.min(8, 2)
The HTML document in EXAMPLE 6.5 includes four different calls to math functions. The page calculates the maximum of two numbers using Math.max, the difference between the numbers using Math.abs, and the square roots of the numbers using Math.sqrt.
EXERCISE 6.9: Confirm the behavior of this page by clicking on the See in Browser button using the default values 100 and 64. Experiment with different numbers in the boxes.
Add functionality to the page by having it also display the minimum of the two numbers.
A function call can appear anywhere in a JavaScript expression. When an expression containing a function call is evaluated, the return value for that call is substituted into the expression. For example, in the assignment below, the return value of the function call Math.sqrt(25) has 1 added to it, resulting in the variable being assigned 6.
addNum = Math.sqrt(25) + 1;
The HTML document in EXAMPLE 6.6 contains more complex function calls. The resulting page has number boxes where you can enter the x and y coordinates of two points, and it then calculates the distance between those points using the formula:
It uses the ** to square the coordinate differences and Math.sqrt to take the square root of their sum.
EXERCISE 6.10: Confirm the behavior of this page by clicking on the See in Browser button using the default coordinates (0,0) and (3,4).
Add functionality to the page by having it also display the minimum of the two numbers.
Recall that a function is a mapping from some number of inputs to a single output. According to this definition, a function can require zero inputs, as demonstrated by the simple user-defined functions from Chapter X4. Those functions accessed any values they needed directly from text boxes. Similarly, the predefined JavaScript function Math.random that takes no inputs but returns a useful result. Every time Math.random is called, a different random number between 0 (inclusive) and 1 (exclusive) is returned. In other words, it returns a random value from the range [0, 1), with the smallest possible value that may be returned being 0 and the largest possible value being very, very close to 1.
For example, the following statements would assign random numbers from the range [0, 1) to the variables randy1 and randy2:
randy1 = Math.random();
randy2 = Math.random();
Since each call to Math.random produces a different random number from the range, the two variables would be assigned different values as a result of these assignments.
Designer Secrets
If we are being technically precise, we should say that the Math.random function returns a pseudo-random number in the range 0 (inclusive) to 1 (exclusive). The value returned by the function is not completely random — it uses a complex algorithm that utilizes numerous factors (including the current time in milliseconds) to select a number that appears random. If you knew the details of the algorithm and the exact values it used, it would be possible to predict the value that would be returned by the function. However, these details are not available to the programmer or the user, so the effect is that the values returned by Math.random appear random.
By itself, the Math.random function is not incredibly useful — there are not very many applications that require a random number between 0 and 1. However, a call to Math.random can be placed in an expression to adjust the range of the numbers. For example, the expression
2*Math.random()
would evaluate to a random number in the range [0..2). One way to see this is to consider the extreme values obtainable from Math.random. The smallest value that Math.random() can return is 0 — if that number is doubled, you still have 0. The largest value that Math.random() can return is 0.999999... — if that number is doubled, you get 1.999999... Any values in between 0 and 1 would yield a number between 0 and 2 when doubled. Similarly,
Math.random() + 1
would evaluate to a random number in the range [1..2). In general:
X*Math.random() + Y
will yield a random number in the range [Y, X+Y). FIGURE 8 contains an interactive visualizer of this formula. You can enter values for X and Y in the boxes, then see a random number and the range that it is taken from.
Enter numbers for the expression involving Math.random(),
then click the button to see the result.
* Math.random() +
FIGURE 8. Interactive random expression visualizer.
EXERCISE 6.11: Confirm the behavior of this page by clicking on the See in Browser button using the default numbers 1 and 0. Experiment with this page to become familiar with the pattern. Predict what numbers would need to be entered in the boxes to generate a random number from each range:
[0, 100) [5, 10) [2, 3) [100, 200) [0.5. 2.5)
Common Errors to Avoid
Because the Math.random function does not require any inputs, you might be tempted to leave off the parentheses when attempting to call it. Although such an omission does not generate an error, the resulting behavior is far from that of the intended call. Without the parentheses, a function name is treated like a variable and evaluates to the function definition. Thus, the assignment
num = Math.random;
would assign the variable num to the function itself. The parentheses after the function name are required if the intent is to call the function and use its returned output. For example, the following statement calls the Math.random function and assigns the returned random number to num.
num = Math.random();
Consider a Pick-4 lottery that utilizes four bins of numbered balls, each containing the same number of balls numbered starting at 1. If there are 10 balls in each of the bins, numbered 1 to 10, then 104 = 10,000 different Pick-4 combinations can potentially be picked. Increasing the number of balls significantly increases the number of possible combinations, which significantly decreases a person's odds of winning. For example, if there are 30 balls to choose from in each bin, labeled from 1 to 30, then 304 = 810,000 different number combinations could be selected.
If we want to simulate picking a random ball out of a bin, we will need an expression that picks a random integer from the range 1..numBalls. As we learned above, the expression:
numBalls*Math.random() + 1
will yield a random real number in the range [1, numBalls+1). If we took the result of this expression and rounded down, we would then produce a random integer between 1 and numBalls, as desired:
Math.floor(numBalls*Math.random() + 1)
The HTML document in EXAMPLE 6.7 that uses this expression to simulate selecting random balls. The resulting page has a box where you can enter the number of balls. When the button is clicked a random Pick-4 selection is displayed, along with the odds of winning.
EXERCISE 6.12: Confirm the behavior of this page by clicking on the See in Browser button using the default number of balls (42). Are all the Pick-4 numbers between 1 and 42? Click on the button multiple times to confirm that the numbers are always within that range.
EXERCISE 6.13: What modifications would need to be made in the HTML document so that the balls are numbered starting at 0? That is, if there were 42 balls then they would be number 0 to 41. Make the modifications and confirm that it works as described.
A variety of measures have been developed to characterize the readability of text. Usually, these measures describe readability in terms of grade level, e.g.,
This fairly simple sentence is rated at a sixth grade reading level.
One such measure is defined by the Flesch-Kincaid Grade Level Formula, which considers the average number of words per sentence and the average number of syllables per word:
gradeLevel = 0.39*averageWordsPerSentence + 11.8*averageSyllablesPerWord - 15.59
For example, the above sentence consisted of 12 words with a total of 18 syllables. Thus, there are (12 / 1) = 12 words per sentence and (18 / 12) = 1.5 syllables per word. According to the Flesch-Kincaid Formula, the reading grade level for this text would be:
gradeLevel = 0.39*12 + 11.8*1.5 - 15.59 = 6.8
We can automate the calculation of reading level using a Web page, in which you enter the number of sentences, words and syllables. At the click of a button, those values can be accessed and the reading level calculated:
The HTML document in EXAMPLE 6.8 implements this page.
There are several details to note about this document and how it implements the desired appearance and behavior of the page.
ids that describe their purpose: sentencesBox contains the number of sentences, wordsBox contains the number of words, and syllablesBox contains the number of syllables. GradeLevel function must complete three logical tasks: (1) access the numbers in the boxes, (2) perform the calculations, and (3) display the results. valueAsNumber attribute is used to access the numerical contents of the boxes. The names of the variables correspond naturally with the box ids.
avgWordsPerSentence and avgSyllablesPerWord) are on the long side, they very clearly denote that value that is stored. The variables are then used to calculate the reading level and store it in a variable (level).innerHTML attribute of outputP. Note that the suffix .toFixed(1) is added to the variable. This ensures that the grade level is rounded to one decimal place (i.e., the nearest tenth) when it is displayed in the page. <input type="number" id="BOX_ID" min=MIN_VAL max=MAX_VAL value=INIT_VAL>If the optional
min and max attributes are present, the number box will be automatically sized to fit that range. valueAsNumber attribute, e.g.,
numBox.valueAsNumber. Math.sqrt), absolute value (Math.abs), maximum (Math.max), minimum (Math.min), round down (Math.floor), round up (Math.ceil), and round to the nearest integer (Math.round). Math.random function takes no inputs and returns a random real number taken from the range [0, 1). Math.random can be embedded in an expression to generate numbers from different ranges. All projects should follow the basic design guidelines described in this and the previous chapters. These include:
head and body sections, with a title element in the head and all the visible elements in the body.head (in script tags). The statement(s) in the function should be indented and the name of the function should be descriptive of its purpose. valueAsNumber attribute and assign the value to a variable. Variables and text boxes should have descriptive names.
PROJECT 6.A: Download a copy of the HTML document in EXAMPLE 6.8 (by clicking on the Download link) and save it on your computer under the name level.html. Next, modify the document so that, instead of calculating the grade level using the Flesch-Kincaid Formula, it instead using the Gunning Fog Formula (which assumes that any word of three or more syllables is complex):
gradeLevel = 0.4*avgWordsPerSentence + 100*(numComplex/numWords)
The page should look similar to the page from EXAMPLE 6.9. That is, it should list the Gunning Fog Formula, it should have number boxes for the three values (number of sentences, number of words, and number of complex words), and a button click should display the grade level, rounded to the nearest tenth. Be sure to add your name to the comment element in the HTML document.
PROJECT 6.B: Recall from Chapter C1 that computer memory is comprised of individual bits of data. A bit (short for binary digit) can store one of only two values, commonly referred to as 0 and 1. However, using two bits, you can represent four different values through the bit patterns 00, 01, 10, and 11. With three bits, you can represent eight different values &mdash via 000, 001, 010, 011, 100, 101, 110, and 111. In general, N bits of memory enable you to represent 2N different values.
Also recall that a byte consists of 8 bits. In other words, 8 bits will fit into 1 byte, 16 bits will fit into 2 bytes, and so on. If the number of bits is not a multiple of 8, then a portion of the last byte will be unused. Thus, 1-8 bits will fit into 1 byte, 9-16 bits will fit into 2 bytes, and so on.
Create a Web page named bits.html that contains a number box where can enter a number. At the click of a button, your page should calculate and display two values: (1) the number of values that can be represented using that number of bits and (2) the number of bytes those bits will fit into (as shown below).
PROJECT 6.C: Consider the task of calculating a student's overall average for a course. Assume the course has a grading scheme with weighted averages for homework, labs, a midterm exam, and a final exam:
Homework 25%
Labs 20%
Midterm 25%
Final exam 30%
To calculate the overall course average, each individual grade is scaled by the appropriate factor and then added to produce the course average. Thus, if a student had a homework average of 92, a lab average of 86, a midterm exam score of 95, and a final exam score of 88, their course average would be:
average = 92*(25/100) + 86*(20/100) + 95*(25/100) + 88*(30/100) = 90.35
Create a Web page named grades.html that can be used to calculate a student's overall average for a course. Your page should contain number boxes where you can enter your homework average, lab average, midterm exam score, and final exam score (as shown below). When the button is clicked, the grade level should be calculated and displayed, rounded to the nearest tenth of a point.
PROJECT 6.D: Weather reports often contain stats that describe current conditions. Two of these stats are dew point (the temperature to which the air must be cooled in order for water vapor to condense into water) and cloud base (the altitude at which clouds will form). Both of these stats can be easily calculated if you know the ground temperature (in degrees Fahrenheit) and the relative humidity:
dewPoint = tempInF - (100-humidity)/5;
cloudBase = 1000*(tempInF - dewPoint)/4.4;
The formulas calculate the dew point in degrees Fahrenheit and the cloud base in feet. If we wanted to also display those stats in Celsius and meters, we would need to know the conversion formulas:
celsius = (fahrenheit - 32) * 5/9
meters = feet / 3.28085
Create a Web page named weather.html that can be used to calculate the dew point and cloud base. Your page should contain number boxes where you can enter the ground temperature and relative humidity (as shown below). When the button is clicked, the dew point and cloud base should be calculated and displayed in both formats, rounded to the nearest tenth.
| ← C6: Algorithms & Programming | ↑ Table of Contents ↑ | C7: Computer Science as a Discipline → |