javascript - Optimum Location for Code Schools -


this taken code wars, not looking cheat or anything. have pretty solved problem i'm unsure if method of determining constitutes best location correct.

var optimumlocation = function(students, locations){   //your solution   var listoflocations = [];     (var key in locations) {     var obj = locations[key];     var totaldistance = 0;     (var = 0; <= students.length-1; i+=1) {       console.log(students[i]);       var location = calculatedistance(students[i],[obj.x, obj.y]);       totaldistance += location;     }      listoflocations.push({id:parseint(key), dist:totaldistance});    }    listoflocations.sort(function(a,b){       return a.dist - b.dist;   });    console.log(listoflocations);   var id = listoflocations[0].id;   return "the best location number " + (id +1) + " coordinates x = " + locations[id].x + " , y = " + locations[id].y; }  function  calculatedistance (loc1, loc2) {   var distx = math.abs(loc1[0] - loc2[0]);   var disty = math.abs(loc1[1] - loc2[1]);     var distance = math.sqrt(distx*distx + disty*disty);    return distance;  }; 

for first test case

optimumlocation([[3,7],[2,2],[14,1]],[{id: 1, x: 3, y: 4}, {id: 2, x: 8, y: 2}]); 

everything fine.

but second test case

optimumlocation([[152,7],[1,211],[14,56],[12,4],[142,7]],[{id: 1, x: 63, y: 55}, {id: 2, x: 55, y: 21},{id: 3, x: 144, y: 12}]); 

the correct location location 2 function thinks location 1. method of using least total distance students travel, location 1 has lowest , therefore should optimum solution on location 2.

any appreciated.

i have managed figure out myself. wondering, on complicated problem. question stated students walk in straight lines, not diagonally. instead of using pythagoras theorem, had add x , y distances find total distance.


Comments

Popular posts from this blog

yii2 - Yii 2 Running a Cron in the basic template -

asp.net - 'System.Web.HttpContext' does not contain a definition for 'GetOwinContext' Mystery -

mercurial graft feature, can it copy? -