array1=(0,0,0);
array2=(1,1,1);
This is not how you declare arrays in javascript.
JavaScript Arrays[
^]
function eucDist (array1, array2)
You never use
array1
and
array2
parameters inside the function.
for(i=0, i=3, i++)
This for loop syntax declaration is incorrect.
JavaScript for Loop[
^]
Moreover, since the body of the loop is empty, it is useless.
return distance
Math.sqrt(Math.pow(x2 - x1, 2) +
Math.pow(y2 - y1, 2) * 1.0);
As stated in solution 1, this will issue a syntax error.
Moreover, this is the formula to compute the euclidean distance in a 2D space, whereas your original arrays suggest that you are dealing with coordinates in a 3D space.
On top of that, multiply any value by 1 is useless and can be simplified by removing the multiplication entirely.
Finally, it is often more efficient, performance-wise, to square a number by multiplying it by itself rather than using
Math.pow()
function.
console.log(array1(0,1,3),array2(4,5,6));
This won't call the
eucDist
function.
I would advise to primarily compute the difference between both coordinates, dimension-by-dimension, and then use these deltas to compute the distance:
function eucDist(lhs, rhs)
{
let deltaX = rhs[0] - lhs[0];
let deltaY = rhs[1] - lhs[1];
let deltaZ = rhs[2] - lhs[2];
return Math.sqrt
(
deltaX * deltaX + deltaY * deltaY + deltaZ * deltaZ
);
}