First project in Javascript: NearestNeighbor Calculator I just finished my first project in Javascript. It takes an array of locations (with coordinates on position 6 and 7) as Input, then the first location is checked against all other locations. After that it takes the closest neighbor and adds it to the route. Then it takes the coordinates of the neighbor which it just found and does same for it (excluding locations that already have a connection). In the end it returns to the starting point. Please give me some hints how i can write better, cleaner and more readable code. Any feedback is appreciated.


var listCoordinates = [
  [1, "München", "Robert-Bürkle-Straße", "1", 85737, "Ismaning", 48.229035, 11.686153, 524, 854],
  [2, "Berlin", "Wittestraße", "30", 13509, "Berlin", 52.580911, 13.293884, 648, 302],
  [3, "Braunschweig", "Mittelweg", "7", 38106, "Braunschweig", 52.278748, 10.524797, 434, 341],
  [4, "Bretten", "Edisonstraße", "2", 75015, "Bretten", 49.032767, 8.698372, 276, 747],
  [5, "Chemnitz", "Zwickauer Straße", "16a", 9122, "Chemnitz", 50.829383, 12.914737, 622, 525],
  [6, "Düsseldorf", "Gladbecker Straße", "3", 40472, "Düsseldorf", 51.274774, 6.794912, 138, 455]
];
//Haversine formula: calulate distance between two locations
const haversineDistance = ([lat1, lon1], [lat2, lon2], isMiles = false) =&gt; {
  const toRadian = angle =&gt; (Math.PI / 180) * angle;
  const distance = (a, b) =&gt; (Math.PI / 180) * (a - b);
  const RADIUS_OF_EARTH_IN_KM = 6371;
  const dLat = distance(lat2, lat1);
  const dLon = distance(lon2, lon1);
  lat1 = toRadian(lat1);
  lat2 = toRadian(lat2);
  //Haversine Formula
  const a = Math.pow(Math.sin(dLat / 2), 2) + Math.pow(Math.sin(dLon / 2), 2) * Math.cos(lat1) * Math.cos(lat2);
  const c = 2 * Math.asin(Math.sqrt(a));
  let finalDistance = RADIUS_OF_EARTH_IN_KM * c;
  if (isMiles) {
    finalDistance /= 1.60934;
  }
  return finalDistance;
};
//heuristic strict nearest neighbor (points that already have a connection do not get calculated)
function nearestNeighbor(coordinates) {
  var route = {
    msg: '',
    distance: 0,
    route: [0]
  }
  var currentPoint = 0;
  for (var i = 0; i &lt;= coordinates.length - 2; i++) {
    var nearestNeighbor = {
      key: 0,
      distance: 0
    }
    var pointaLat = coordinates[currentPoint][6],
      pointaLon = coordinates[currentPoint][7];
    for (var j = 0; j &lt;= coordinates.length - 1; j++) {
      if (j != currentPoint) {
        if (!route.route.includes(j)) {
          var pointbLat = coordinates[j][6],
            pointbLon = coordinates[j][7];
          var currentDistance = haversineDistance([pointaLat, pointaLon], [pointbLat, pointbLon], 0);
          if (nearestNeighbor.distance === 0 || nearestNeighbor.distance &gt; currentDistance) {
            nearestNeighbor.key = j;
            nearestNeighbor.distance = currentDistance;
          }
        }
      }
    }
    route.distance += nearestNeighbor.distance;
    route.route.push(nearestNeighbor.key)
    currentPoint = nearestNeighbor.key;
  }
  //return to start point
  var pointaLat = coordinates[0][6],
    pointaLon = coordinates[0][7],
    pointbLat = coordinates[route.route[route.route.length - 1]][6],
    pointbLon = coordinates[route.route[route.route.length - 1]][7];
  route.distance += haversineDistance([pointaLat, pointaLon], [pointbLat, pointbLon], 0);
  route.route.push(0);
  route.distance = Math.round((route.distance + Number.EPSILON) * 100) / 100;
  route.msg = "Die kürzeste Strecke beträgt " + route.distance + " km";
  return route;
}

console.log(nearestNeighbor(listCoordinates));
Are you hoping for the arc length around a sphere or the Earth? What about going through the earth in a straight line? Currently your math solves the sphere but not Earth.
The earth has 2 radi that are used to solve the arc length.
R = √ [ (r1² * cos(B))² + (r2² * sin(B))² ] / [ (r1 * cos(B))² + (r2 * sin(B))² ]
B = Latitude
R = Radius
r1 = radius at equator
r2 = radius at the poles

But your Haversine method is otherwise correct =D