```
// Standard pythagoras
const pythagoras = (a, b) => Math.sqrt(a * a + b * b); // c = Math.sqrt(a*a + b*b)
// Using it to establish a simple distance calculation
// Here [x0, y0] .. define coordinate pairs in 2 dimensions
const distance2D = (x0, y0, x1, y1) => pythagoras(x1 - x0, y1 - y0);
// Expanding it to N dimensions
// vector is a list
// vector.reduce executes a reducer function (that you provide) on each element of the array, resulting in single output value. *1
const pythagorasND = (vector) => Math.sqrt(
vector.reduce((sum, value) => sum + value * value)
);
// Expanding the distance function to N dimensions
// p0 and p1 are lists or vectors defining poinst in a respective dimension
// p0.map creates a new array populated with the results of calling a provided function on every element in the calling array
const distanceND = (p0, p1) => pythagorasND(
p0.map((value, index) => value - p1[index])
);
// Keep in mind, for constant dimension sizes, it is always more performant to manually write out the distance function for the given dimension
const listA = [2,3,4,5];
const listB = [1,2,3,4];
console.log(distanceND(listA, listB)); // Prints: 2.0
// Practical Tasks
// Task 1: Create a function dedicated to finding the distance between two coordinate points in three dimensions by finishing these functions
// Replace 0s with your code
const pythagoras3D = (a, b, c) => 0;
const distance3D = (x0, y0, z0, x1, y1, y2) => pythagoras3D(
0, 0, 0
);
// Results -- Should output: ~1.732
console.log("Task 1: expected", 1.732);
console.log(
"Task 1: result",
distance3D(
1, 2, 3, // First coordinate [x0, y0, z0]
2, 3, 4 // Second coordinate [x1, y1, z1]
)
);
// Task 2: Find the closest point to our current position by finishing this formula
const ourPosition = [65,25];
const points = [
[33,144],
[155,55],
[66,177],
[76,12],
[33,45]
];
let minimumDistance = 999999;
let closestPoint = null;
// Iterate through all the points, this can also be represented using array reduce, but is generally less readable that way
for (let i = 0; i < points.length; i++) {
let point = points[i];
let distance = 0;
if (distance < minimumDistance) { // if the distance is less than the previous closest position, set the position and new minimum distance
closestPoint = point;
minimumDistance = distance;
}
}
console.log("Task 2: expected", [76, 12], "with a distance of", 17.029);
console.log("Task 2: result", "The closest point is ", closestPoint, "with a distance of", minimumDistance);
```

This Pen doesn't use any external CSS resources.

This Pen doesn't use any external JavaScript resources.