| f r o m |
|||||
|---|---|---|---|---|---|
| 0) Indianapolis | 1) New York | 2) Tokyo | 3) London | ||
| 0) Indianapolis | 0 | 648 | 6476 | 4000 | |
| 1) New York | 648 | 0 | 6760 | 3470 | |
| 2) Tokyo | 6476 | 6760 | 0 | 5956 | |
| 3) London | 4000 | 3470 | 5956 | 0 | |
//create a 2-dimension array
distance = new Array (
new Array (0, 648, 6476, 4000),
new Array (648, 0, 6760, 3470),
new Array (6476, 6760, 0, 5956),
new Array (4000, 3470, 5956, 0)
);
cityName = new Array("Indianapolis", "New York", "Tokyo", "London");
function getCity(){
// presents a list of cities and gets a number corresponding to
// the city name
var theCity = ""; //will hold the city number
var cityMenu = "Please choose a city by typing a number: \n";
cityMenu += "0) Indianapolis \n";
cityMenu += "1) New York \n";
cityMenu += "2) Tokyo \n";
cityMenu += "3) London \n";
theCity = prompt(cityMenu);
return theCity;
} // end getCity
function main(){
var output = "";
from = getCity();
to = getCity();
result = distance[from][to];
output = "the distance from " + cityName[from];
output += " to " + cityName[to];
output += " is " + result;
alert(output);
} // end main
Andy Harris