JSFiddle - React, Tailwind, and code Playground
by thewolff
JavaScript
function Point (name, x,y) {
this.name = name;
this.x = x;
this.y = y;
this.findClosest = function(arr){
for(var i = 0, len = arr.length, d, d1, closest = 'undefined'; i < len; i++){
d = Math.sqrt(Math.pow(this.x-arr[i].x, 2)+Math.pow(this.y-arr[i].y, 2));
if(i === 0 || d < d1){
d1 = d;
closest = arr[i]
}
if(i === len-1){
return closest;
}
}
}
}
var p1 = new Point("P1",5,6);
var p2 = new Point("P2",6,7);
var p3 = new Point("P3",10,15);
var p4 = new Point("P4",60,70);
p1.findClosest([p2,p3,p4]);