I encountered a problem with my project where this function is returning NaN. In the past, I was able to resolve a similar issue by setting a variable equal to 0. However, that solution is not working in this case.
var distanceArray = [
[0, 62.3, 58.8, 44.6, 33.2],
[62.3, 0, 37.9, 65.3, 60.1],
[58.8, 37.9, 0, 40.5, 78.5],
[44.6, 65.3, 40.5, 0, 77.6],
[33.2, 60.1, 78.5, 77.6, 0]
]
var route = [0, 1];
function routeFunction() {
var totalDistance = 0;
for (var i = 0; i < route.length; i++) {
totalDistance += distanceArray[route[i]][route[i + 1]];
}
return totalDistance;
}
console.log(routeFunction());