settingsAccountsettings
By using our mini forum, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy
Menusettings

Q: Point Distance (task with JS Class)

+3 votes

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

asked in JavaScript category by user nikole

1 Answer

+1 vote

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));

 

answered by user richard8502
...