Online Documentation Server
 ПОИСК
ods.com.ua Web
 КАТЕГОРИИ
Home
Programming
Net technology
Unixes
Security
RFC, HOWTO
Web technology
Data bases
Other docs

 


 ПОДПИСКА

 О КОПИРАЙТАХ
Вся предоставленная на этом сервере информация собрана нами из разных источников. Если Вам кажется, что публикация каких-то документов нарушает чьи-либо авторские права, сообщите нам об этом.




Previous Table of Contents Next

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.

Output

The 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:


Figure 10-2.  A dialog box that prompts the user to enter the desired measurement system.


Figure 10-3.  A dialog box that prompts the user to enter this or her weight in kilograms.


Figure 10-4.  A short sentence based on a collection of calculations.


Figure 10-5.  The “scientific” results.

Base Converter

Task

This script performs two alternative functions:

  • Accepts a number in any base and converts it to a different base, provided by the user
  • Accepts a series of numbers and converts them to all even bases between binary (2) and hexadecimal (16)

    Note that the script can handle only positive integers.

    Algorithm


    Note:  This script is designed to demonstrate various features of JavaScript. It does not always use the most efficient functions to reach this goal. In addition, as of Netscape Navigator 3.0, this script is totally unnecessary. The reason is that Netscape Navigator 3.0 supports a new method, toString(radix), that enables you to automatically convert a number to any base. For example, 5.toString(2) == "101". Some other functions in the script are also unnecessary, such as power(), because built-in methods are equivalent to them (in this case, Math.pow()).


    At first, the script asks the user if he or she wants to create a table or to convert a single value. If the user selects the first function, the number is converted to a decimal base and then to the specified base. If the number is represented in a decimal base, it is directly converted to the desired base. If the number is to be represented in decimal notation, only that conversion is executed. Octal and hexadecimal integers take advantage of their special conventions in JavaScript so their conversion to decimal is automatic. The script functions according to common conversion methods.

    Just as a reminder, representation of a number in a system with base N may only consist of digits that are less than (not equal to) N.

    Applied to the construction of numbers, the positional principle operates as follows: the sequence of digits ...srqp is defined to signify a number which is equal to the sum of products involving powers of a number a, in which a is called the base or the radix; that is, the position of each of the coefficients [pi], q, r, s, ... is associated, in reverse order, to the representation srqp with a zero, first, second, third, ... power of the base a. The number of distinct numerals required in this notation is readily seen to be a, commonly represented by the known decimal digits, and additional letters from the Latin alphabet. The basic equation for such conversion is:

    p * a0 + q * a1 + r * a2 + s * a3 + . . .

    Notice that the digits in the original numbers appear in a reverse order.

    For example, to find the decimal representation M of 5128 we use the following calculation:

    M10 = 2 * 80 + 1 * 81 + 5 * 82 = 2 + 8 + 320 = 33010




With any suggestions or questions please feel free to contact us