Skip to content

Instantly share code, notes, and snippets.

@lastday154
Created September 5, 2018 03:14
Show Gist options
  • Save lastday154/50c5d15d1ad490df80f937f63594797d to your computer and use it in GitHub Desktop.
Save lastday154/50c5d15d1ad490df80f937f63594797d to your computer and use it in GitHub Desktop.
min distance
function compare(a,b) {
if (a.distance < b.distance) {
return -1;
}
if (a.distance > b.distance) {
return 1;
}
return 0;
}
function ClosestXdestinations(numDestinations, allLocations, numDeliveries)
{
// WRITE YOUR CODE HERE
let distances = [];
allLocations.forEach((location, index) => {
const distance = Math.sqrt(Math.pow(location[0], 2) + Math.pow(location[1], 2));
distances.push({index, distance});
})
distances = distances.sort(compare);
const indexLocations = distances.slice(0, numDeliveries).map((item) => {
return item.index;
});
const result = [];
for(let i=0; i< numDeliveries; i++) {
result.push(allLocations[indexLocations[i]]);
}
return result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment