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

Example 10-1. A script that calculates the user’s height and weight percentile.

Analysis

The script looks rather long, but it is simple and easy to understand, because much of it is pure data and a repeated if - else statement.

convertInput()

function convertInput() {
 weightInput *= 0.45359
 heightInput *= 2.54
}

The convertInput() function does not accept any arguments, because it modifies two global variables. The function converts weightInput and heightInput from the English system to the metric one. weightInput is converted to kilograms, and heightInput is converted to centimeters. The function does not return any value because both variables are global.

getHeight(sex, age, height)

function getHeight(sex, age, height) {
 height = Math.round(height)
 var height5 = 0
 var height50 = 0
 if (age == 2) {
  height50 = 87
  height5 = 82
 } else
 if (age == 3) {
  height50 = 95
  height5 = 90
 } else
 if (age == 4) {
  height50 = 102
  height5 = 95
 } else
 if (age == 5) {
  height50 = 109
  height5 = 101
 } else
 if (age == 6) {
  height50 = 114
  height5 = 107
 } else
 if (age == 7) {
  height50 = 120
  height5 = 112
 } else
 if (age == 8) {
  height50 = 126
  height5 = 118
 } else
 if (age == 9) {
  height50 = 131
  height5 = 121
 } else
 if (age == 10) {
  height50 = 137
  height5 = 127
 } else
 if (age == 11) {
  height50 = 143
  height5 = 131
 } else
 if (sex == "f") {
  if (age == 12) {
   height50 = 150
   height5 = 140
  } else
  if (age == 13) {
   height50 = 157
   height5 = 145
  } else
  if (age == 14) {
   height50 = 160
   height5 = 148
  } else
  if (age == 15) {
   height50 = 162
   height5 = 150
  } else
  if (age == 16) {
   height50 = 162
   height5 = 151
  } else
  if (age == 17) {
   height50 = 163
   height5 = 153
  } else
  if (age == 18) {
   height50 = 164
   height5 = 154
  }
 } else
 if (age == 12) {
  height50 = 150
  height5 = 137
 } else
 if (age == 13) {
  height50 = 156
  height5 = 142
 } else
 if (age == 14) {
  height50 = 162
  height5 = 148
 } else
 if (age == 15) {
  height50 = 168
  height5 = 155
 } else
 if (age == 16) {
  height50 = 174
  height5 = 160
 } else
 if (age == 17) {
  height50 = 175
  height5 = 165
 } else
 if (age == 18) {
  height50 = 176
  height5 = 165
 }
 var percent = (height – height5) * (50 – 5)
 percent /= (height50 – height5) + 5
 return percent
}

This function accepts three arguments: the user’s sex, age, and height. The variable height, holding the user’s height, is rounded off to the nearest integer. The height is already represented in centimeters, even if the user chooses to use the English system.

After rounding off the height (age is already an integer, rounded off by a global statement), two new local variables are declared and initialized with the numeric value 0: height50, and height5. The variable height50 is intended to hold the average height measurement for a male or female of the user’s specified age. The other variable, height5 holds the height of the 5th percentile. We use an if - else construction to assign these two variables the appropriate values. Each if statement checks if the user’s age is equal to the age in the condition expression (e.g., age == 10). The else statements instruct JavaScript to continue executing the if statements in order, as long as the value of age does not match the value against which it is tested. From a quick look at the graph, one can find that males and females have the same height until age 11. Therefore, the first eleven if statements apply to both female and male users. Here is a short section from the function:

if (age == 2) {
 height50 = 87
 height5 = 82
} else
if (age == 3) {
 height50 = 95
 height5 = 90
} else
if (age == 4) {
 height50 = 102
 height5 = 95
} else
...

We’ll follow this script segment to see what it does. At first, JavaScript checks if age is equal to 2. If it is, the value 87 is assigned to height50, and 82 is assigned to height5. These are the approximate values from the graph. If age is not equal to 2, the expression

age == 2

evaluates to false, and JavaScript carries out the else statement. An “improper” indentation is used in this script, because the nested if - else statements are extremely deep, and it would be unreasonable to add an additional tab to each statement. The last statement would have been preceded by approximately 17 tabs—impossible!

Another part of the function is the section where the female statistics are different from the male ones:

if (age == 11) {
  height50 = 143
  height5 = 131
 } else
 if (sex == "f") {
  if (age == 12) {
   height50 = 150
   height5 = 140
  } else
  .
  .
  .
  } else
  if (age == 18) {
   height50 = 164
   height5 = 154
  }
 } else
 if (age == 12) {
  height50 = 150
  height5 = 137
 } else
 if (age == 13) {
  height50 = 156
  height5 = 142
 } else

Previous Table of Contents Next


With any suggestions or questions please feel free to contact us