Connecting data from 2 arrays

I have a little bit hard question, but I try to explain it:

So, I have 2 arrays. First array - array of cities. The second is the matrix with distances between this cities;

var cities  = ['Gamburg', 'Dresden', 'Berlin', 'Bayern', 'Dortmund'];
var distanceMatrix = [
   [0, 131, 523, 251, 600],
   [131, 0, 785, 123, 541],
   [252, 785, 0,  241, 741],
   [251, 123, 241, 0,  632],
   [600, 541, 741, 632, 0]
];

I need to generate a train with randomly choosen department and arrival point, with is not a problem. The problem is how to connect this 2 points with distance matrix correctly. If its possible. For example: distance between cities[1] and cities[3] accords to distanceMatrix[1][3] etc. So I need a function which randomly pick 2 towns and show distance between them according to distanceMatrix.

P.S. 2 days of hard mind work, and all I've done - is just 5 switch() functions, which is wrong way to complete this task. Thank you !

Upvotes: 1

Views: 72

Answers (2)

Philippe Delteil
Philippe Delteil

Reputation: 1265

Well, if I understood the problem correctly, you look for a way to form and access the matrix.

Let's first see the problem as indexes.

var cities = ['Gamburg', 'Dresden', 'Berlin', 'Bayern', 'Dortmund'];

cities[0] = Gamburg, cities[1] = Dresden, and so on.

The question is how to pick to cities at random and return the distance between the two.

function randomCities() {

    while(true)
    {
        //Let's generate a random number between 0 and 4. 
         var randomCity_A = Math.floor((Math.random() * 4) + 0); 

         var randomCity_B =  Math.floor((Math.random() * 4) + 0);
         // add a condition that cities are not the same

         if ( randomCity_A != randomCity_B){    
             var distance = distanceMatrix[randomCity_A][randomCity_B];
             return "Distance between " + cities[randomCity_A] +" and " + 
                   cities[randomCity_A] +": "+distance;
            }
      }
}

console.log(randomCities());
console.log(randomCities());

Upvotes: 0

Nina Scholz
Nina Scholz

Reputation: 386654

You could take Array#indexOf for getting the index as accessor for the matrix.

var cities = ['Gamburg', 'Dresden', 'Berlin', 'Bayern', 'Dortmund'],
    distances = [[0, 131, 523, 251, 600], [131, 0, 785, 123, 541], [252, 785, 0, 241, 741], [251, 123, 241, 0, 632], [600, 541, 741, 632, 0]];

function getDistance(city1, city2) {
    return distances[cities.indexOf(city1)][cities.indexOf(city2)];
}

console.log(getDistance('Dresden', 'Berlin'));
console.log(getDistance('Dortmund', 'Bayern'));

An advanced solution could use an object with the indices.

var cities = ['Gamburg', 'Dresden', 'Berlin', 'Bayern', 'Dortmund'],
    indices = { Gamburg: 0, Dresden: 1, Berlin: 2, Bayern: 3, Dortmund: 4 },
    distances = [[0, 131, 523, 251, 600], [131, 0, 785, 123, 541], [252, 785, 0, 241, 741], [251, 123, 241, 0, 632], [600, 541, 741, 632, 0]];

function getDistance(city1, city2) {
    return distances[indices[city1]][indices[city2]];
}

console.log(getDistance('Dresden', 'Berlin'));
console.log(getDistance('Dortmund', 'Bayern'));

Upvotes: 2

Related Questions