Click here to Skip to main content
15,891,951 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
JavaScript
array1=(0,0,0);
array2=(1,1,1);
function eucDist (array1, array2){
 let distance => for(i=0, i=3, i++){
   
 }
  return distance
  Math.sqrt(Math.pow(x2 - x1, 2) +  
                Math.pow(y2 - y1, 2) * 1.0);
}

console.log(array1(0,1,3),array2(4,5,6));


What I have tried:

This is what I have so far and I am not sure what I have to do to make it work.
Posted
Updated 23-Feb-20 21:16pm
v2
Comments
Richard MacCutchan 20-Feb-20 9:57am    
Do you know the formula for calculating the euclidean distance? If not, Goggle will probably find it for you.

JavaScript
array1=(0,0,0);
array2=(1,1,1);
This is not how you declare arrays in javascript.
JavaScript Arrays[^]

JavaScript
function eucDist (array1, array2)
You never use array1 and array2 parameters inside the function.

JavaScript
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.

JavaScript
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.

JavaScript
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:
JavaScript
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
   );
}
 
Share this answer
 
Look at your code.
How is the line that calculates the Euclidean distance supposed to be executed?
Either there is a return line above it that is missing a semicolon, or the expression it is returning is not a valid expression.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900