|
An alert box then displays the sentence. JavaScript displays another alert box with the “scientific” percentile, weight, and height results, if the user wishes to see that information. This is done via an if statement and a confirm() method as its condition. The last operation in this function is the disclaimer-like message. Global Statements
var sex = prompt("Enter sex ((m)ale or (f)emale):", "") var ageInput = parseInt(prompt("Enter age in years (min.=2):", "")) ageInput = Math.round(ageInput) var systemMessage = "Whould you like to use the (m)etric system " systemMessage += "or the (e)nglish one?" var system = prompt(systemMessage, "m") var heightUnit = (system == "m") ? "centimeters" : "inches" var weightUnit = (system == "m") ? "kilograms" : "pounds" var heightInput = prompt("Enter height in " + heightUnit + ":", "") heightInput = parseInt(heightInput) var weightInput = prompt("Enter weight in " + weightUnit + ":", "") weightInput = parseInt(weightInput) if (system == "e") convertInput() if (ageInput > 18) var age = 18 else if (ageInput < 2) var age = 2 else var age = ageInput var heightPer = getHeight(sex, age, heightInput) var weightPer = getWeight(sex, age, weightInput) heightPer = (heightPer < 1) ? 1 : heightPer heightPer = (heightPer > 99) ? 99 : heightPer weightPer = (weightPer < 1) ? 1 : weightPer weightPer = (weightPer > 99) ? 99 : weightPer printResult(heightPer, weightPer, sex, age)
At first, JavaScript asks the user to enter his or her sex as a single letter (e.g., “f”, “m”). Notice that none of the input values in the script are evaluated, so the user must enter a valid value. Data evaluation is discussed later in the book. JavaScript then asks the user for his or her age, and converts it to a number (all input from dialog boxes’ fields are in the form of strings) with the parseInt() function. This built-in function is also discussed later in the book. The user’s age is rounded off to the nearest integer. The user is then prompted to choose the desired measurement system: metric or English. The appropriate values are assigned to heightUnit and weightUnit (e.g., centimeters, inches, kilograms, pounds) via a conditional expression. Before calling any function, JavaScript asks the user for his or her height and weight. Each value is converted to a number using the parseInt() function. At this point, the height and weight values are converted to metric units, via convert(), if they are in English units. The value of the variable age is assigned 18 if it is greater than 18, and 2 if it is less than 2. These values are the limits, and out-of-range values would not work with the getHeight() and getWeight() functions. The getHeight() and getWeight() functions are called in order and return the computed percentile. The percentile is cut to its limits if it is out of range. The minimum percentile is 1; the maximum is 99. The last statement calls the printResult() to print the results as explained earlier. OutputThe height and weight calculator script “listens” and “speaks” only via dialog boxes. Here are a few dialog boxes generated throughout the course of the script’s execution: Base ConverterTaskThis script performs two alternative functions:
|
||||||||||||||||||||
With any suggestions or questions please feel free to contact us |