How is the following program calculated to output 7.2111? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

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

8th Jul 2019, 4:04 PM
eMBee
eMBee - avatar
1 Answer
+ 6
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)
8th Jul 2019, 4:14 PM
Airree
Airree - avatar