Conversion between Distance Units

Virtual Earth uses four different types of distance units; meters, feet, kilometers, and miles. Conversion between distance units consists of multiplying or dividing a number by a constant. There are several ways we can handle this situation, we can have twelve constants to represent every possible conversion between these four units, or we can make a function that uses three constants and calculates the conversion. We will implement the second method.

Theory

For the theory we will first go through the twelve different conversions and the connection between each unit.

Meters to Kilometers = Meters -> Kilometers =

Kilometers to Meters = Kilometers -> Meters =

Feet to Miles = Feet -> Miles =

Miles to Feet = Miles -> Feet =

Kilometers to Miles = Kilometers -> Miles =

Miles to Kilometers = Miles -> Kilometers =

By combining the above six conversions we can make all the rest of the possible conversions between the four distance units.

Meters to Miles = Meters -> Kilometers -> Miles =

Miles to Meters = Miles -> Kilometers -> Meters =

Feet to Kilometers = Feet -> Miles -> Kilometers =

Kilometers to Feet = Kilometers -> Miles -> Feet =

Feet to Meters = Feet -> Miles -> Kilometers -> Meters =

Meters to Feet = Meters -> Kilometers -> Miles -> Feet =

Application

To apply the conversions we will use if statements that will determine if the conversion that is being performed uses one of the six main conversion types. The function to convert between distance units will take in two parameters, a distance and the conversion type. The distance will be a number and the conversion type will be a string that represents the type of conversion that is to be made. The syntax for the conversion type parameter will consist of two units separated by the number two. For example, the conversion type for meters (m) to feet (ft) would be “m2ft”. The following four units are used for the conversion type parameter: meter (m), feet (ft), kilometers (km), and miles (mi).

```function convertDistance(distance, conversionType){    //meters to kilometers
if(conversionType=="m2km"||conversionType=="m2mi"||conversionType=="m2ft")         distance /= 1000;        //kilometers to meters
if(conversionType=="ft2m"||conversionType=="mi2m"||conversionType=="km2m")        distance *= 1000;                //feet to miles
if(conversionType=="ft2mi"||conversionType=="ft2m"||conversionType=="ft2km")        distance /= 5280;        //miles to feet
if(conversionType=="mi2ft"||conversionType=="m2ft"||conversionType=="km2ft")        distance *= 5280;            //kilometers to miles
if(conversionType=="m2mi"||coversionType=="m2ft"||conversionType=="km2ft"||conversionType=="km2mi")        distance /= 1.609344;        //miles to kilometers
if(conversionType=="mi2m"||conversionType=="ft2m"||conversionType=="ft2km"||conversionType=="mi2km")        distance *= 1.609344;            return distance;}```

This function takes in conversion types that consist of a from unit and to unit which is separated by the number two. The following are the unit short forms used:

• meters: m
• feet: ft
• miles: mi
• kilometers: km

By using the match regular expression method of JavaScript we can simplify the above algorithm by matching the starting and ending characters of the conversion type. This algorithm reduces to the following:

```function convertDistance(distance, conversionType){    //meters to kilometers
if(conversionType.match("^m2")=="m2")         distance /= 1000;        //kilometers to meters
if(conversionType.match("2m\$")=="2m")        distance *= 1000;                //feet to miles
if(conversionType.match("^ft2")=="ft2")        distance /= 5280;        //miles to feet
if(conversionType.match("2ft\$")=="2ft")        distance *= 5280;            //kilometers to miles
if(conversionType=="m2mi"||coversionType=="m2ft"||conversionType=="km2ft"||conversionType=="km2mi")        distance /= 1.609344;        //miles to kilometers
if(conversionType=="mi2m"||conversionType=="ft2m"||conversionType=="ft2km"||conversionType=="mi2km")        distance *= 1.609344;            return distance;}```

The following is a list of conversion types that can be passed:

• meters to kilometers: "m2km"
• meters to miles: "m2mi"
• meters to feet: "m2ft"
• feet to meters: "ft2m"
• feet to kilometers: "ft2km"
• feet to miles: "ft2mi"
• kilometers to miles: "km2mi"
• kilometers to feet: "km2ft"
• kilometers to meters: "km2m"
• miles to feet: "mi2ft"
• miles to meters: "mi2m"
• miles to kilometers: "mi2km"