I am really new to HTML and Javascript. I would appreciate any help with the code I'm writing.
I am working on a calculator to display the distance between airports. The user will have five "text" inputs, so they will write airport codes, say MAD, JFK. The result will be the distance between Madrid and JFK. The user can add several airports, such as BCN MAD JFK ORD, and the result should be the total addition of all legs (result = BCN-MAD + MAD-JFK + JFK-ORD).
For my purpose, it is also important that the distances are indicated by me (therefore, I do not need to "calculate" the distances between the coordinates).
What I did was that I created variables called "leg1,2,3,4" that connect each airport, so leg1 could be, for example, "BCNMAD", and then using if I can get the distance for that leg. Finally, there is a variable totalDistance to add all the distances between the legs.
Since each leg must be calculated separately and added to the other legs, I copied the if for each individual leg.
Everything seems to be working fine, the problem is that I added only 8 airports. The actual requirement will be about 200 airports. And since each leg has its own if set, I could get about 800 IF.
I was wondering if there is a better way to do this? Also, I don't know if there is a limit on the number of if ?
As I said, I am very new to HTML and Javascript, so I am sure there are much better options for this, but I have been stuck with this problem for several days and this is the best solution I could come up with.
Therefore, any advice or help would be greatly appreciated. I have included the code below.
Regards, Mike
function displayDistance() { var leg1 = {route:document.getElementById("air1").value + document.getElementById("air2").value , distance:""}; var leg2 = {route:document.getElementById("air2").value + document.getElementById("air3").value , distance:""}; var leg3 = {route:document.getElementById("air3").value + document.getElementById("air4").value , distance:""}; var leg4 = {route:document.getElementById("air4").value + document.getElementById("air5").value , distance:""}; if (leg1.route == "AGPMAD" || leg1.route == "MADAGP") {leg1.distance = 430;} if (leg1.route == "BCNMAD" || leg1.route == "MADBCN") {leg1.distance = 483;} if (leg1.route == "LHRMAD" || leg1.route == "MADLHR") {leg1.distance = 1246;} if (leg1.route == "CDGMAD" || leg1.route == "MADCDG") {leg1.distance = 1065;} if (leg1.route == "JFKMAD" || leg1.route == "MADJFK") {leg1.distance = 5777;} if (leg1.route == "ORDJFK" || leg1.route == "JFKORD") {leg1.distance = 1191;} if (leg1.route == "JFKMCO" || leg1.route == "MCOJFK") {leg1.distance = 1520;} if (leg1.route == "JFKIAD" || leg1.route == "IADJFK") {leg1.distance = 367;} if (leg2.route == "AGPMAD" || leg2.route == "MADAGP") {leg2.distance = 430;} if (leg2.route == "BCNMAD" || leg2.route == "MADBCN") {leg2.distance = 483;} if (leg2.route == "LHRMAD" || leg2.route == "MADLHR") {leg2.distance = 1246;} if (leg2.route == "CDGMAD" || leg2.route == "MADCDG") {leg2.distance = 1065;} if (leg2.route == "JFKMAD" || leg2.route == "MADJFK") {leg2.distance = 5777;} if (leg2.route == "ORDJFK" || leg2.route == "JFKORD") {leg2.distance = 1191;} if (leg2.route == "JFKMCO" || leg2.route == "MCOJFK") {leg2.distance = 1520;} if (leg2.route == "JFKIAD" || leg2.route == "IADJFK") {leg2.distance = 367;} if (leg2.route == "AGPMAD" || leg2.route == "MADAGP") {leg2.distance = 430;} if (leg3.route == "BCNMAD" || leg3.route == "MADBCN") {leg3.distance = 483;} if (leg3.route == "LHRMAD" || leg3.route == "MADLHR") {leg3.distance = 1246;} if (leg3.route == "CDGMAD" || leg3.route == "MADCDG") {leg3.distance = 1065;} if (leg3.route == "JFKMAD" || leg3.route == "MADJFK") {leg3.distance = 5777;} if (leg3.route == "ORDJFK" || leg3.route == "JFKORD") {leg3.distance = 1191;} if (leg3.route == "JFKMCO" || leg3.route == "MCOJFK") {leg3.distance = 1520;} if (leg3.route == "JFKIAD" || leg3.route == "IADJFK") {leg3.distance = 367;} if (leg4.route == "AGPMAD" || leg4.route == "MADAGP") {leg4.distance = 430;} if (leg4.route == "BCNMAD" || leg4.route == "MADBCN") {leg4.distance = 483;} if (leg4.route == "LHRMAD" || leg4.route == "MADLHR") {leg4.distance = 1246;} if (leg4.route == "CDGMAD" || leg4.route == "MADCDG") {leg4.distance = 1065;} if (leg4.route == "JFKMAD" || leg4.route == "MADJFK") {leg4.distance = 5777;} if (leg4.route == "ORDJFK" || leg4.route == "JFKORD") {leg4.distance = 1191;} if (leg4.route == "JFKMCO" || leg4.route == "MCOJFK") {leg4.distance = 1520;} if (leg4.route == "JFKIAD" || leg4.route == "IADJFK") {leg4.distance = 367;} var totalDistance = leg1.distance + leg2.distance + leg3.distance + leg4.distance; document.getElementById("distanceresult").innerHTML = totalDistance; }
<span>Route: </span> <input type="text" class="airinput" id="air1" value="" required=""/> <input type="text" class="airinput" id="air2" value="" required=""/> <input type="text" class="airinput" id="air3" value="" required=""/> <input type="text" class="airinput" id="air4" value="" required=""/> <input type="text" class="airinput" id="air5" value="" required=""/> <br/> <input type="button" onClick="displayDistance();" value="Calculate Distance"/><br/> <span>The total distance is: </span><span id="distanceresult"></span><br/>