user13433290
user13433290

Reputation:

The specified value "NaN" cannot be parsed, or is out of range

I am getting this The specified value "NaN" cannot be parsed or is out of range. error on my console, I am new to javascript and I am unable to solve this issue, a little help would be great.

I am sharing my Script part with you;

 <script language="JavaScript">
  
  function quad() {
  
  a=eval(document.form.A.value);
  
  b=eval(document.form.B.value);
  
  c=eval(document.form.C.value);
  
  x1=-b/2/a+Math.pow(Math.pow(b,2)-4*a*c,0.5)/2/a;
  
  x2=-b/2/a-Math.pow(Math.pow(b,2)-4*a*c,0.5)/2/a;

  if (x1 == "NaN") x1="Imag.!";

  if (x2 == "NaN") x2="Imag.!";
  
  document.getElementById("ans-1").value = x1;
  
  document.getElementById("ans-2").value = x2;
  
  }
  
  // End -->
  
  </script>

Upvotes: 1

Views: 34322

Answers (1)

AdamExchange
AdamExchange

Reputation: 1301

x1 == "NaN" is not checking if the value is NaN. isNaN(x1) is the preferred way to check if it is NaN.

I would also be careful using eval in JavaScript. parseFloatwould be the preferred way.

Upvotes: 9

Related Questions