0

I've got a HTML5 form with 2 dropdown inputs. The form is used to determine the distance between 2 company locations.

The current script works, but its not really good, because this would work for a few locations, but not for 23 locations as it is in my case.

How can I make an "cross reference function" that just checks a long list of vars and values?

Script:

The javascript function to check the chosen dropdown options and return the value:

inputField is the field that shows the distance to the user.

function initCalc(inputField,select1,select2) {
         select1.change(function(){
            inputField.val(calculateValue(select1.val(),select2.val()));
        });
        select2.change(function(){
            inputField.val(calculateValue(select1.val(),select2.val()));
        });
    }

"Calculating" the distance:

function calculateValue(firstOp,secondOp) {
        var kmCost = 0.19;
            if (firstOp == "Company1" && secondOp == "Company1") { var total = 0; }
            else if (firstOp == "Company1" && secondOp == "Company2") { var total = 205; }
            else if (firstOp == "Company1" && secondOp == "Company3") { var total = 174; }
            else if (firstOp == "Company1" && secondOp == "Company4") { var total = 3; }
            else if (firstOp == "Company1" && secondOp == "Company5") { var total = 21; }
            else if (firstOp == "Company1" && secondOp == "Company6") { var total = 129; }
            else if (firstOp == "Company2" && secondOp == "Company1") { var total = 205; }
            else if (firstOp == "Company2" && secondOp == "Company2") { var total = 0; }
            else if (firstOp == "Company2" && secondOp == "Company3") { var total = 11; }
            //etc...

            else { var total = 0; }
            var total = total * kmCost;
            return total.toFixed(2);
}

EDIT

With help of @JRK I came with the following, but I think I'm doing something wrong to get the value of the dropdown list from the object?

function calculateValue(firstOp,secondOp) {
    var kmCost = 0.19;
        var myObject = {Amsterdam : {distances : {Paris : 1, NewYork : 2}}, 
                        Paris : {distances : {Amsterdam : 1, NewYork : 3}},
                        NewYork : {distances : {Amsterdam : 2, Paris : 3}}
                    };
        var total = myObject.firstOp.distances.secondOp;

    var total = total * kmCost;
    return total.toFixed(2);
}

EDIT2

I've got it! Thanks to this post.

function calculateValue(firstOp,secondOp) {
    var kmCost = 0.19;
        var myObject = {Amsterdam : {distances : {Paris : 1, NewYork : 2}}, 
                        Paris : {distances : {Amsterdam : 1, NewYork : 3}},
                        NewYork : {distances : {Amsterdam : 2, Paris : 3}}
                    };
        var total = myObject[firstOp]['distances'][secondOp];

    var total = total * kmCost;
    return total.toFixed(2);
}
Bldjef
  • 138
  • 10
  • How have you worked out the total? Do you have coordinates for the locations? Or are you wanting to keep the calculated distance, but refactor all of the else ifs..? – JRK Jun 21 '19 at 10:37
  • @JRK The totals are defined at the and of each `if` rule. – Bldjef Jun 21 '19 at 10:39
  • 1
    Sorry, I'm asking whether instead of defining the total for each combination, you rather take the coordinates and work out the distance based on 2 pairs of coordinates, like Company 1 lon - lat with Company 2 lon - lat - it'll avoid all the else ifs, or if want to keep the defined total, then perhaps use an object of companies with distances of others. – JRK Jun 21 '19 at 10:43
  • Yes, preferably I want to keep the defined totals. So an object would indeed be better. I'm looking into it now to figure out how to use that in my function. – Bldjef Jun 21 '19 at 10:47
  • Cool, if you need any help then I'll provide a pseudo answer perhaps. But i'd use something like: `{Company1 : {distances : {Company2 : 100, Company3: 100}}}` access like: `myObject.Company1.distances.Company2` – JRK Jun 21 '19 at 10:52
  • Okay, I'm going to need some help with that :) If you could help me with a part of the object code and implementation in the `calculateValue` function that would be great! Edit, oh you just did :D – Bldjef Jun 21 '19 at 10:55
  • @JRK I've edited my post, could you check what I'm doing wrong? – Bldjef Jun 21 '19 at 11:28
  • Found the solution, see edit2! – Bldjef Jun 21 '19 at 12:04

1 Answers1

0

I've got it! Thanks to this post.

function calculateValue(firstOp,secondOp) {
    var kmCost = 0.19;
        var myObject = {Amsterdam : {distances : {Paris : 1, NewYork : 2}}, 
                        Paris : {distances : {Amsterdam : 1, NewYork : 3}},
                        NewYork : {distances : {Amsterdam : 2, Paris : 3}}
                    };
        var total = myObject[firstOp]['distances'][secondOp];

    var total = total * kmCost;
    return total.toFixed(2);
}
Bldjef
  • 138
  • 10