Wasif Jalal Galib
Wasif Jalal Galib

Reputation: 13

Why am i getting this NaN?

I made a javascript with simple math but i keep getting NaN (Not a Number) as the output.

This is the original equation https://upload.wikimedia.org/math/9/5/d/95d176cfd7d8ab9e4df1977f0926d1d0.png

And here is my code:

<html>
<head>
<title></title>
<script>
function showheight() {
var chi = 23 * (Math.PI) / 180;
var vara = 6378137;
var varb = 6356752.31425;

var varR = (Math.sqrt(((Math.pow(((Math.pow(vara, 2)) * (Math.cos(chi))), 2)) + (Math.pow(((Math.pow(varb, 2)) * (Math.sin(chi)))))) / ((Math.pow((vara * (Math.cos(chi))), 2)) + (Math.pow((varb * (Math.sin(chi))), 2)))));

document.getElementById("radius").innerHTML = varR;
}

function addLoadEvent(func) {
  var oldonload = window.onload;
  if (typeof window.onload != 'function') {
    window.onload = func;
  } else {
    window.onload = function() {
      if (oldonload) {
        oldonload();
      }
      func();
    }
  }
}

addLoadEvent(showheight);
</script>
</head>

<body>
<p id="radius"></p>
</body>
</html>

P.S.: I am a newbie.

Upvotes: 0

Views: 149

Answers (1)

Jaromanda X
Jaromanda X

Reputation: 1

The problem is here

var varR = (Math.sqrt(((Math.pow(((Math.pow(vara, 2)) * (Math.cos(chi))), 2)) + (Math.pow(((Math.pow(varb, 2)) * (Math.sin(chi)))))) / ((Math.pow((vara * (Math.cos(chi))), 2)) + (Math.pow((varb * (Math.sin(chi))), 2)))));
                                                                                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^


Math.pow(((Math.pow(varb, 2)) * (Math.sin(chi))))

only one parameter for that Math.pow

var varR = (Math.sqrt(((Math.pow(((Math.pow(vara, 2)) * (Math.cos(chi))), 2)) + (Math.pow(((Math.pow(varb, 2)) * (Math.sin(chi))), 2))) / ((Math.pow((vara * (Math.cos(chi))), 2)) + (Math.pow((varb * (Math.sin(chi))), 2)))));

gives 6374895.33901514 as an answer - is that what you expect?

Demo - JsFiddle

Upvotes: 1

Related Questions