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 else statement checks if the user is a female. A true condition instructs JavaScript to execute a command block containing the female-specific if - else segment. JavaScript ignores the command block if the condition is false, i.e., the user is a male. The next area of the function is specific for males. The entire if - else execution terminates after the user’s age is matched. The maximum age inside the function is assumed to be 18. If the user enters a higher number, it is converted to 18 in the global section of the script before this function is invoked.

The following graph schematically explains the percentile calculation:


Figure 10-1.  The percentile graph.

Finding the y (percentile) value of a given x (height) value is a problem you probably solved in one of your geometric analysis classes:

percent = (height – height5) * (50 – 5) / (height50 – height5) + 5

The script uses this exact formula.

getWeight(sex, age, weight)

This function is the same as the previous one, except that it deals with the user’s weight, instead of his or her height.

printResult(height, weight, sex, age)

function printResult(height, weight, sex, age) {
 var heightAdj = ""
 var weightAdj = ""
 var ageAdj = ""
 var sexAdj = ""
 var gradeAdj = ""
 var grade = 0
 var propWeight = weight / height
 if (height > 70) {
heightAdj = "tall"
grade += 2
 } else
  if (height < 30) {
 heightAdj = "short"
 grade += 1
  } else {
 heightAdj = "medium-height"
 grade += 3
  }
 if (propWeight > 2) {
weightAdj = "over-weight"
grade += 1
 } else
  if (propWeight < 0.5) {
 weightAdj = "slim"
 grade += 2
  } else {
 weightAdj = "medium-weight"
 grade += 3
  }
 ageAdj = ageInput + "-year-old"
 if (grade >= 5)
gradeAdj = "great-looking"
 else
  if (grade <= 2)
   gradeAdj = "awkward-looking"
  else
   gradeAdj = "fine-looking"
 sexAdj = (sex == "f") ? "female" : "male"
 var finalMessage = "You are a " + heightAdj + ", "
 finalMessage += weightAdj + ", " + gradeAdj + " "
 finalMessage += ageAdj + " " + sexAdj + "."
 alert(finalMessage)
 if (confirm("Are you interested in scientific results?")) {
  scMessage = "height = " + Math.round(height)
  scMessage +="%\rweight = " + Math.round(weight)
  scMessage += "%\rweight/height = " + propWeight
  alert(scMessage)
 } else
  if (grade <= 4)
   alert("Good idea!")
 var notice = "Thank you for using the JavaScript weight "
 notice += "and height calculator. All calculations are "
 notice += "done according to the child-development graph "
 notice += "in \"Compton's Encyclopedia\". We apologize "
 notice += "if you were insulted by the comments -- that "
 notice += "was not our intention. We used them to demonstrate "
 notice += "various JavaScript scripting techniques."
 alert(notice)
}

This function is more interesting than the previous ones because it does not have any repeating segment. At the beginning of the function, five adjectives are declared and initialized to an empty string—a clue that they are used to hold string values later in the function. Another variable, grade, is declared and initialized to 0, indicating that it is used to hold a numeric value. It is especially important to assign the value 0 to this variable because it is accumulative, and any other value would generate wrong results. A new variable named propWeight is declared and initialized to the user’s weight percentile, divided by his or her height percentile. This measure normalizes the person’s weight by his or her height, giving a more accurate picture of the person’s physical appearance (you can’t expect an 8-foot person to be of average weight, can you?!). The following script segment assigns a value to the variable heightAdj according to the height percentile computed by the getHeight()function:

if (height > 70) {
 heightAdj = "tall"
 grade += 2
   } else
 if (height < 30) {
heightAdj = "short"
grade += 1
 } else {
heightAdj = "medium-height"
grade += 3
 }

Notice that the variable height, like weight, sex, and age, is a parameter, so its name is not required to be identical to its counterpart global one.

The preceding script segment assigns one of three values to the variable heightAdj: "tall", "short", or "medium-height". If the height percentile is over 70%, the person is considered tall and if it is below 30%, the person is considered short. In all other cases (30% - 70%) the selected adjective is "medium-height". Notice that along with the adjective, a number is added to the variable grade, representing the number of points awarded to the given characteristic. For example, a tall person receives two out of three possible points in the height category. Medium-height people receive the maximum three points, whereas short people receive the minimum one point. All of this is done with a nested if - else structure, where each statement is a command block consisting of two nested statements. WeightAdj is assigned similarly.

The adjective describing the person’s age is created by concatenating his or her age to the string "-year-old ". Notice the number to string conversion (cast) in this statement.

An overall adjective is assigned to gradeAdj according to the final grade. (Do not take any result as an insult or as medical advice, because it almost never returns a realistic one.)

Finally, the user’s sex is assigned to sexAdj, according to his or her input at the beginning.

All adjectives are now ready, so a comment is constructed with the following format:

"You are a "+heightAdj+","+weightAdj+",
	"+gradeAdj+", "+ageAdj+" "+sexAdj+"."

Previous Table of Contents Next


With any suggestions or questions please feel free to contact us