Jump to content

NaN


alzami
 Share

Recommended Posts

<!doctype html><html><head><meta charset="utf-8"><title>Untitled Document</title><style>.its{	width:32%;	font-size:20px;	color:blue;	margin:0 auto;	}#gets{	width:289px;	height:50px;	font-size:20px;	color:black;	background-color:white;	}#divs{	width:289px;height:50px;font-size:20px;color:red;background-color:gray;display:none;position:absolute;top:13px;left:10px;border:1px solid black;}</style></head><body style="position:relative;"><div id="divs"><a href="#" onClick="solve();">ax^2+bx+c=0</a></div><form style="background-color:grey;display:inline-block;height:350px;width:300px;padding:10px;"><input type="text" id="gets" value="calculator(press CLR to begin)"></br><input type="button" value="0" onClick="calc(this.value);" class="its"><input type="button" value="1" onClick="calc(this.value);" class="its" ><input type="button" value="+" onClick="calc(this.value);" class="its"> <input type="button" value="2" onClick="calc(this.value);" class="its"><input type="button" value="3" onClick="calc(this.value);" class="its"><input type="button" value="-" onClick="calc(this.value);" class="its"><input type="button" value="4" onClick="calc(this.value);" class="its"><input type="button" value="5" onClick="calc(this.value);" class="its"><input type="button" value="/" onClick="calc(this.value);" class="its" ><input type="button" value="6" onClick="calc(this.value);" class="its"><input type="button" value="7" onClick="calc(this.value);" class="its"><input type="button" value="*" onClick="calc(this.value);" class="its"><input type="button" value="8" onClick="calc(this.value);" class="its"><input type="button" value="9" onClick="calc(this.value);" class="its" ><input type="button" value="=" onClick="ev('gets');" class="its"><input type="button" value="sqrt" onClick="sqr('gets');" class="its" ><input type="button" value="%" onClick="calc(this.value);" class="its"><input type="button" value="CLR" onClick="clr('gets');"class="its" ><input type="button" value="ln" onClick="lnit('gets');" class="its" ><input type="button" value="sin" onClick="trig('gets',Math.sin);" class="its" ><input type="button" value="tan" onClick="trig('gets',Math.tan);" class="its"><input type="button" value="cos" onClick="trig('gets',Math.cos);"class="its" ><input type="button" value="cosec(x)" onClick="rev_trig('gets',Math.sin);" class="its" ><input type="button" value="cot(x)" onClick="rev_trig('gets',Math.tan);" class="its"><input type="button" value="sec(x)" onClick="rev_trig('gets',Math.cos);"class="its" ><input type="button" value="x^(y)" onClick="power('gets');"class="its" ><input type="button" value="log2" onClick="log2('gets');"class="its" ><input type="button" value="log10" onClick="log10('gets');"class="its" ><input type="button" value="solve" onClick="solve_show('divs');"class="its" ><input type="button" value="X" onClick="calc(this.value);"class="its" ></form><script>var i=0;var t=300;function calc(i){document.getElementById("gets").value +=i;				}function ev(el){var c=eval(document.getElementById("gets").value);	document.getElementById("gets").value=c;}function clr(el){	var m=document.getElementById(el);	m.value="";}function sqr(el){var c=document.getElementById(el);var v=Math.sqrt(c.value);c.value=v;}function lnit(el){	var c=document.getElementById(el);	var v=Math.log(c.value);	c.value=v;}function trig(el,oprtr){	var m=document.getElementById(el);	var p=(m.value)*( Math.PI/180);	var v=oprtr(p);	m.value=v;}function power(el){	var m=document.getElementById(el);	var v=prompt("put power : ");	var p=Math.pow(m.value,v);	m.value=p;}function log2(el){	var m=document.getElementById(el);		for(;{         var d=Math.pow(2,i);				 if(d==m.value){			 m.value=i;			 i=0;			 break;		 }		  i++;	}}function log10(el){	var m=document.getElementById(el);		for(;{         var d=Math.pow(10,i);				 if(d==m.value){			 m.value=i;			 i=0;			 break;		 }		  i++;	}}	function rev_trig(el,oprtr){	var m=document.getElementById(el);	var p=(m.value)*( Math.PI/180);	var d=oprtr(p);	var g=1/d;	m.value=g;}function solve_show(el){   var m=document.getElementById(el);   m.style.display="block";}function solve(){	var m=document.getElementById("divs");	m.style.display="none";	var c=document.getElementById("gets");	var a=prompt("put coeff of x^2");	var b=prompt("put coeff of x");	var c=prompt("put value of c");	var p=(b*b-4*a*c);	var nom=Math.sqrt(p);	var de_nom=2*a;	var x1=(-b+nom)/de_nom;	var x2=(-b-nom)/de_nom;	c.value="x1 :"+ x1 + " x2 :"+ x2 ;}</script></body></html>

problem is....

this script always giving x1 and x2 value NaN.what might be the reason?

function solve(){	var m=document.getElementById("divs");	m.style.display="none";	var c=document.getElementById("gets");	var a=prompt("put coeff of x^2");	var b=prompt("put coeff of x");	var c=prompt("put value of c");	var p=(b*b-4*a*c);	var nom=Math.sqrt(p);	var de_nom=2*a;	var x1=(-b+nom)/de_nom;	var x2=(-b-nom)/de_nom;	c.value="x1 :"+ x1 + "x2 :"+ x2 ;}

is it ok?

c.value="x1 :"+ x1 + " x2 :"+ x2 ;
Edited by alzami
Link to comment
Share on other sites

It might be because you're operating with strings.

 

Try casting them to numbers:

var a=prompt("put coeff of x^2");var b=prompt("put coeff of x");var c=prompt("put value of c");a = parseFloat(a); // Cast to numberb = parseFloat(B); // Cast to numberc = parseFloat(c); // Cast to number
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...