Skip to content

Instantly share code, notes, and snippets.

@amypellegrini
Last active October 27, 2016 14:38
Show Gist options
  • Save amypellegrini/382f2f091ad9df9c067dac072e6098ba to your computer and use it in GitHub Desktop.
Save amypellegrini/382f2f091ad9df9c067dac072e6098ba to your computer and use it in GitHub Desktop.
Distance formula in Node.js
let x1 = process.argv[2],
y1 = process.argv[3],
x2 = process.argv[4],
y2 = process.argv[5];
/**
* Return the distance between two points for the given coordinates.
*
* @param {Number} x1 - x coordinate for the first point.
* @param {Number} y1 - y coordinate for the fisrt point.
* @param {Number} x2 - x coordinate for the second point.
* @param {Number} y2 - y coordinate for the second point.
* @returns {Number} Distance between p1 and p2.
*/
function distance(x1, y1, x2, y2) {
return Math.sqrt(Math.pow(x1 - x2, 2) + Math.pow(y1 - y2, 2));
}
console.log(distance(x1, y1, x2, y2));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment