-6

I would like to calculate distance from origin for some points. I don't know how to do that in Javascript.

If x and y are the co-ordinates of a point distanceFromOrigin=sqrt(x^2 + y^2).

So far, below is the code I could get running in HTML:

<html>
<head>
<script id="vertices">
float distanceFromOrigin;

void
main()
{
distanceFromOrigin=(x * x) +(y * y);


}
</script>
</head>
</html>

How to add a square root to the above variable distanceFromOrigin. And also is there a power function instead of doing x*x?

srk
  • 427
  • 4
  • 11
  • 27

2 Answers2

1

Try this..

Check out this Fiddle

function calculate() {
  var x = document.getElementById("x").value;
  var y = document.getElementById("y").value;
  console.log("Distance from origin to x and y is : " + Math.round(Math.sqrt(Math.pow(x, 2) + Math.pow(y, 2))));
}
<div>
  <input type="text" id="x" placeholder="x">
  <input type="text" id="y" placeholder="y">
  <input type="button" value="Find distance from origin" onclick="calculate();">
</div>
dippas
  • 58,591
  • 15
  • 114
  • 126
Malik Naik
  • 1,472
  • 14
  • 16
-2
Math.sqrt(9); // --> 3
Math.pow(4, 3); // --> 64

W3 Schools

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
SDekov
  • 9,276
  • 1
  • 20
  • 50