Skip to content

Instantly share code, notes, and snippets.

@petergi
Created April 8, 2026 15:24
Show Gist options
  • Select an option

  • Save petergi/a7dfaac4af9f31bf11fbd8a3c5cee21a to your computer and use it in GitHub Desktop.

Select an option

Save petergi/a7dfaac4af9f31bf11fbd8a3c5cee21a to your computer and use it in GitHub Desktop.
// Calculates the distance between two points in any number of dimensions.
// - Use `Object.keys()` and `Array.prototype.map()` to map each coordinate to its difference between the two points.
// - Use `Math.hypot()` to calculate the Euclidean distance between the two points.
const euclideanDistance = (a, b) => Math.hypot(...Object.keys(a).map(k => b[k] - a[k]));
euclideanDistance([1, 1], [2, 3]); // ~2.2361
euclideanDistance([1, 1, 1], [2, 3, 2]); // ~2.4495
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment