Chapter 10
JavaScript by Example—a Review
Height and Weight Calculator
The Task
This script accepts the user’s height and weight, and displays a greeting including a
comment on the user’s weight status. The comment is based on the height and weight
measurements of the user, the proportion between them, his or her age and sex, and an
overall result. It optionally displays the “scientific” results, including height and
weight percentile and their quotient.
The Algorithm
At first, the script asks the user for his or her sex and age (two years is the
minimum). If the age exceeds 18, it is converted to 18 in order to fit the pre-entered
database. Then, the script asks the user to select the desired measurement system: metric
or English. The selected units are reflected in the messages asking the user for his or
her height and weight (e.g., “Enter height in centimeters:”). If the user chooses
English, the weight and height are converted to the metric system (cm, kg), after which
their percentiles are calculated. Based on the user’s age, the script finds the height
and weight of the 5th and 50th percentiles. Using a simple interpolation, the user’s
specific height and weight percentiles are calculated. The script checks that the computed
percentile is between 1 and 99, and out-of-range results are converted to the nearest
valid value (1 or 99). JavaScript displays a comment according to computed percentiles.
The script also gives the user an option to view the more “scientific” results,
including the actual numeric percentiles and their quotient. The weight and height
percentiles were copied from Compton’s Encyclopedia.
The Script
<HTML>
<HEAD>
<TITLE>Body height and weight calculator</TITLE>
<SCRIPT LANGUAGE="JavaScript">
<!--
// Returns the height measurement (50% is
// average...), according to the given
// sex, age, and actual height in cm
/////////////////////////////////////////
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
}
// Returns the weight measurement (50% is
// average...), according to the given
// sex, age, and actual weight in kg
/////////////////////////////////////////
function getWeight(sex, age, weight) {
weight = Math.round(weight)
var weight5 = 0
var weight50 = 0
if (age == 2) {
weight50 = 12
weight5 = 10
} else
if (age == 3) {
weight50 =14
weight5 = 12
} else
if (age == 4) {
weight50 = 16
weight5 = 14
} else
if (age == 5) {
weight50 = 18
weight5 = 15
} else
if (age == 6) {
weight50 = 20
weight5 = 17
} else
if (age == 7) {
weight50 = 22
weight5 = 18
} else
if (age == 8) {
weight50 = 25
weight5 = 20
} else
if (age == 9) {
weight50 = 29
weight5 = 22
} else
if (sex == "f") {
if (age == 10) {
weight50 = 32
weight5 = 25
} else
if (age == 11) {
weight50 = 37
weight5 = 27
} else
if (age == 12) {
weight50 = 41
weight5 = 30
} else
if (age == 13) {
weight50 = 46
weight5 = 34
} else
if (age == 14) {
weight50 = 50
weight5 = 38
} else
if (age == 15) {
weight50 = 53
weight5 = 40
} else
if (age == 16) {
weight50 = 56
weight5 = 43
} else
if (age == 17) {
weight50 = 57
weight5 = 45
} else
if (age == 18) {
weight50 = 57
weight5 = 46
}
} else
if (age == 10) {
weight50 = 24
weight5 = 31
} else
if (age == 11) {
weight50 = 35
weight5 = 27
} else
if (age == 12) {
weight50 = 40
weight5 = 30
} else
if (age == 13) {
weight50 = 46
weight5 = 35
} else
if (age == 14) {
weight50 = 51
weight5 = 38
} else
if (age == 15) {
weight50 = 57
weight5 = 44
} else
if (age == 16) {
weight50 = 62
weight5 = 48
} else
if (age == 17) {
weight50 = 67
weight5 = 53
} else
if (age == 18) {
weight50 = 69
weight5 = 55
}
var percent = (weight – weight5) * (50 – 5)
percent /= (weight50 – weight5) + 5
return percent
}
// Creates a comment according to the
// height, weight, age, sex, and a
// computed overall calculation. Also
// displays a disclaimer. It optionally
// displays the "scientific" results
/////////////////////////////////////////
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)
}
// converts weight and height from
// English system to metric system.
/////////////////////////////////////////
function convertInput() {
weightInput *= 0.45359
heightInput *= 2.54
}
// Global statements to receive input
// and call functions
/////////////////////////////////////////
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 = "Would 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)
// -->
</SCRIPT>
</HEAD>
<BODY>
</BODY>
</HTML>