3
04/11/2024 6:51 am
Topic starter
Write a JS class that represents a Point. It has x and y coordinates as properties, that are set through the constructor, and a static method for finding the distance between two points, called distance().
Input:
The distance() method should receive two Point objects as parameters.
Output:
The distance() method should return a number, the distance between the two point parameters.
Submit the class definition as is, without wrapping it in any function.
Examples:
Sample Input:
let p1 = new Point(5, 5); let p2 = new Point(9, 8); console.log(Point.distance(p1, p2));
Output:
5
1 Answer
2
04/11/2024 6:52 am
My class Point:
class Point {
constructor(x, y) {
this.x = x;
this.y = y;
}
static distance(a, b) {
const dx = a.x - b.x;
const dy = a.y - b.y;
return Math.sqrt(dx * dx + dy * dy);
}
}
let p1 = new Point(1, 3);
let p2 = new Point(5, -1);
console.log(p1);
console.log(p2);
console.log(Point.distance(p1, p2));
