+2

How is the following program calculated to output 7.2111?

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.hypot(dx, dy); } } const p1 = new Point(7, 2); const p2 = new Point(3, 8); console.log(Point.distance(p1, p2));

7/8/2019 4:04:52 PM

Mofey

1 Answer

New Answer

+5

dx and dy calculates the distances, obviously. It returns Math.hypot(dx, dy). This function is basically the same as: √(dx² + dy²) (calculates the distance with the Pythagorean theorem). I'm guessing it was multiple inputs. You could count up, that dx and dy was 4 and 6 (doesn't matter if positive or negative). 4² + 6² equals 50, and you can then guess that its square root is a little bigger than seven (√49)