Guest User

JTQ's Distance Calculator

a guest
Sep 18th, 2015
109
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
HTML 1.23 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  5. <title>JTQ Distance Calculator</title>
  6.  
  7. <script>
  8.     var cityName = ["Indianapolis", "New York", "Tokyo", "London"];
  9.     var distances = [
  10.         [0, 648, 6476, 4000],
  11.         [648, 0, 6760, 3470],
  12.         [6476, 6760, 0, 5956],
  13.         [4000, 3470, 5956, 0],
  14.     ];
  15.  
  16.     var cityMenu = "\n\n";
  17.     for (var i=0; i < cityName.length; i++)
  18.        cityMenu += i + ") " + cityName[i] + "\n";
  19.  
  20.    function GetCity(heading)
  21.    {
  22.        var theCity;
  23.        theCity = prompt(heading + cityMenu);
  24.        //Should check that theCity is valid!
  25.        return theCity;
  26.    }
  27.  
  28.    function main()
  29.    {
  30.        var output;
  31.        var fromCity = GetCity("Please choose a starting city by typing in a number:");
  32.        var toCity = GetCity("Please choose an ending city by typing in a number:");
  33.        var result = distances[fromCity][toCity];
  34.        output = "The distance from " + cityName[fromCity];
  35.        output += " to " + cityName[toCity];
  36.        output += " is " + result + " miles.";
  37.        alert (output);
  38.    }
  39. </script>
  40. </head>
  41. <body onload="main();">
  42. <h2>JTQ's distance calculator</h2>
  43. </body>
  44. </html>
Advertisement
Add Comment
Please, Sign In to add comment