Tell me what is the best and easiest way to turn the table of tariff zones of "New Mail" by the link http://novaposhta.ua/uploads/misc/doc/taryfni_zony.pdf into conditions in pure Javascript?

For those who do not know: in the left vertical column is taken the area from which one person sends the goods, and in the right horizontal bar is taken the area where the other person accepts the sent goods. At the intersection of the regions, the number of the tariff zone is obtained (the farther one region is from another, the larger the number).

The regions of Ukraine were previously translated into numbers ie Kiev region, for example, is 9-ka (9th in order in the table), Odessa 14, Dneprovskaya 3, Lvov 12, etc.

The first thing that comes to mind is:

if(numberOfSenderRegion == 9 && numberOfReceiverRegion == 3 || numberOfSenderRegion == 3 && numberOfReceiverRegion == 9 || numberOfSenderRegion == 9 && numberOfSenderRegion == 14 || numberOfReceiverRegion == 14 && numberOfReceiverRegion == 9 || numberOfSenderRegion == 9 && numberOfSenderRegion == 12 || numberOfReceiverRegion == 12 && numberOfReceiverRegion == 9) { var numberOfZone = 4; } 

and so it turns out who knows how many similar conditions need to be prescribed only for the 4th zone

Is there a simpler way?

ps. zone 0 (sending within one region) is not considered; there is one condition enough:

 if(numberOfSenderRegion == numberOfReceiverRegion) { var numberOfZone = 0; } 

    1 answer 1

    You can, there is a simple option, though you have to sweat a little at first. You need to make yourself a hash of the following form

     {"1-0": 4, "2-0": 4, "2-1": 5, .... } 

    Explanation of the hash. The key is the area numbers in the minus (of course, you can take two letters each, then the key will look somewhere like "od-ky"). Value is just a value. Since the table seems to be symmetrical, only half of the values ​​can be saved.

    Now how to use. After two areas have been received, we translate them into a numerical or alphabetic value (most likely it is for you to make the html version itself :)). We check that this is not the same region and change keys in places, if necessary (I, in my example, saved the larger key first, but this is a convention). Further, having two area codes, we form keys and look for them in the table.

    This approach is good enough: - the search is fast, somewhere behind the logarithm (exactly faster than the if / switch series) - it is easy to modify arbitrary changes. You only need to find it in the list :) - the table is easy to store in a separate file.

    Disadvantages: - the table needs to be generated. But this is done once. - you need to decide what is important - memory or clear keys.

    In table 24 area. 1 + 2 + 3 + ... + 23 = 23 * 12 = 276 keys. Even if you store three letters in the name of the area, it is less than 4kb text. So everything is ok.

    How I would generate a table. I would carefully transfer it to excel and then save it to csv (or another convenient format). And then a simple script in your favorite language (I have a pearl :)) would write a ready-made js generator. Although I think there are experts who will do this on vbscript right in Excel.

    • Thanks, but unfortunately not so strong in JS to figure out how to get such a hash. Could you, at least by the example of one pair of areas, illustrate the whole code? - stckvrw
    • And what exactly did I give in the answer? 0 is Vinnytsia, 1 is Volyn, 2 is Dnipro. Or do you want ready-made code and the entire table at once? - KoVadim
    • No, you do not quite understand. How to get such a hash or how to work with it? (I do not know how to put it exactly). - stckvrw