2

Author Topic: javascript NaN debugging problem  (Read 6956 times)

0 Members and 1 Guest are viewing this topic.

Offline AdminTopic starter

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,658
  • Helpful? 169
    • Society of Robots
javascript NaN debugging problem
« on: October 20, 2007, 01:22:29 PM »
For some reason I keep getting a NaN error. If I replace line 43 with something like var RMF=5; then the error goes away.

I have it running on this page:
http://www.societyofrobots.com/RMF_calculator.shtml

It is also giving me another error relating to the cookie code that I also can't figure out . . . Anyone know what Im doing wrong?

Code: [Select]
<!--#include file="header.html" -->

<center><font face=Verdana><b>RMF CALCULATOR</b></font></center>
<p>

<!-- for math help http://www.pageresource.com/jscript/jmath.htm -->

<script language="javascript">

function tw_calc() {

//collect user entered data
var mass=document.getElementById("massText").value;
var velocity=document.getElementById("velText").value;
var acceleration=document.getElementById("accText").value;
var efficiency=document.getElementById("effText").value;
var angle=document.getElementById("angText").value;
var rotation=document.getElementById("rotText").value;

//correct for units (change to english units)
var tunit=document.getElementById("massSelect").value;
if (tunit=="met"){
mass=mass*2.20462262;
}
var tunit=document.getElementById("velSelect").value;
if (tunit=="met"){
velocity=velocity*3.2808399;
}
var tunit=document.getElementById("accSelect").value;
if (tunit=="met"){
acceleration=acceleration*3.2808399;
}
var tunit=document.getElementById("rotSelect").value;
if (tunit=="minute"){
rotation=rotation/60;
}
var efficiency=efficiency*.01;

//alert('2 mass='+mass+' acceleration='+acceleration+' velocity='+velocity+' efficiency='+efficiency+' angle='+angle+' rotation='+rotation);

//calculating
var gravity=32;
var RMF=mass*(acceleration+gravity*Math.sin(angle*Math.PI/180))*velocity*(1/efficiency)/(2*Math.PI);

var momentum=mass*velocity;
var diameter=velocity/(rotation*Math.PI);

//alert('3 gravity='+gravity+' RMF='+RMF+' momentum='+momentum+' diameter='+diameter+' test='+test);

//output correct for units
var tunit=document.getElementById("RMFSelect").value;
if (tunit=="met"){
RMF=RMF*0.138254954;
}
var tunit=document.getElementById("momentumSelect").value;
if (tunit=="met"){
momentum=momentum*0.138254954;
}
var tunit=document.getElementById("diameterSelect").value;
if (tunit=="met"){
diameter=diameter*0.3048;
}

//output
document.getElementById("RMFOutput").value=RMF.toPrecision(3);
document.getElementById("momentumOutput").value=momentum.toPrecision(3);
document.getElementById("diameterOutput").value=diameter.toPrecision(3);

}

function tw_save(){
userCookie.put("tw_text1", document.getElementById("massText").value)
userCookie.put("tw_text2", document.getElementById("velText").value)
userCookie.put("tw_text3", document.getElementById("accText").value)
userCookie.put("tw_text4", document.getElementById("effText").value)
userCookie.put("tw_text5", document.getElementById("angText").value)
userCookie.put("tw_text6", document.getElementById("rotText").value)
userCookie.put("tw_sel1", document.getElementById("massSelect").value)
userCookie.put("tw_sel2", document.getElementById("velSelect").value)
userCookie.put("tw_sel3", document.getElementById("accSelect").value)
userCookie.put("tw_sel4", document.getElementById("rotSelect").value)
userCookie.write()
}

userCookie  = new cookieObject("tw_form", 365, "/", "tw_text1", "tw_text2", "tw_text3", "tw_text4", "tw_text5", "tw_text6", "tw_sel1", "tw_sel2", "tw_sel3", "tw_sel4")
if (userCookie.found) {
document.getElementById("massText").value=userCookie.get("tw_text1")
document.getElementById("velText").value=userCookie.get("tw_text2")
document.getElementById("accText").value=userCookie.get("tw_text3")
document.getElementById("effText").value=userCookie.get("tw_text4")
document.getElementById("angText").value=userCookie.get("tw_text5")
document.getElementById("rotText").value=userCookie.get("tw_text6")
document.getElementById("massSelect").value=userCookie.get("tw_sel1")
document.getElementById("velSelect").value=userCookie.get("tw_sel2")
document.getElementById("accSelect").value=userCookie.get("tw_sel3")
document.getElementById("rotSelect").value=userCookie.get("tw_sel4")
}
else {
window.status=window.status+" cookie not found"
}

tw_calc();
window.onunload=function() { tw_save(); }
</script>


<P><STRONG>Inputs:<BR>
</STRONG>
<TABLE id="Table1" cellSpacing="1" cellPadding="1" width="300" bgColor="#ffffcc" border="1">
<TR>
<TD>Mass</TD>
<TD><INPUT id="massText" onkeyup="tw_calc()" type="text" size="8" value="2" name="massText"></TD>
<TD><SELECT id="massSelect" onchange="tw_calc()" name="massSelect">
<OPTION value="eng" selected>lbs</OPTION>
<OPTION value="met">kg</OPTION>
</SELECT></TD>
</TR>
<TR>
<TD>Desired Velocity</TD>
<TD><INPUT id="velText" onkeyup="tw_calc()" type="text" size="8" value=".5" name="velText"></TD>
<TD><SELECT id="velSelect" onchange="tw_calc()" name="velSelect">
<OPTION value="eng" selected>ft/s</OPTION>
<OPTION value="met">m/s</OPTION>
</SELECT></TD>
</TR>
<TR>
<TD>Desired Acceleration</TD>
<TD><INPUT id="accText" onkeyup="tw_calc()" type="text" size="8" value=".5" name="accText"></TD>
<TD><SELECT id="accSelect" onchange="tw_calc()" name="accSelect">
<OPTION value="eng" selected>ft/s^2</OPTION>
<OPTION value="met">m/s^2</OPTION>
</SELECT></TD>
</TR>
<TR>
<TD>Expected Efficiency</TD>
<TD><INPUT id="effText" onkeyup="tw_calc()" type="text" size="8" value="75" name="effText"></TD>
<TD>%</TD>
</TR>
<TR>
<TD>Incline Angle</TD>
<TD><INPUT id="angText" onkeyup="tw_calc()" type="text" size="8" value="10" name="angText"></TD>
<TD>degrees</TD>
</TR>
<TR>
<TD>Motor Rotation Speed</TD>
<TD><INPUT id="rotText" onkeyup="tw_calc()" type="text" size="8" value="1" name="rotText"></TD>
<TD><SELECT id="rotSelect" onchange="tw_calc()" name="rotSelect">
<OPTION value="second" selected>rps</OPTION>
<OPTION value="minute">rpm</OPTION>
</SELECT></TD>
</TR>
</TABLE>
</P>
<P><STRONG><STRONG>RMF Results</STRONG><BR>
</STRONG>
<TABLE id="Table4" cellSpacing="1" cellPadding="1" width="300" bgColor="#ccffcc" border="1">
<TR>
<TD>RMF</TD>
<TD><INPUT id="RMFOutput" onkeyup="tw_calc()" type="text" size="8" name="RMFOutput"></TD>
<TD><SELECT id="RMFSelect" onchange="tw_calc()" name="RMFSelect">
<OPTION value="eng" selected>lb * ft * rps</OPTION>
<OPTION value="met">kg * m * rps</OPTION>
</SELECT></TD>
</TR>
<TR>
<TD>Momentum</TD>
<TD><INPUT id="momentumOutput" onkeyup="tw_calc()" type="text" size="8" name="momentumOutput"></TD>
<TD><SELECT id="momentumSelect" onchange="tw_calc()" name="momentumSelect">
<OPTION value="eng" selected>lb * ft / s</OPTION>
<OPTION value="met">kg * m / s</OPTION>
</SELECT></TD>
</TR>
<TR>
<TD>Wheel Diameter</TD>
<TD><INPUT id="diameterOutput" onkeyup="tw_calc()" type="text" size="8" name="diameterOutput"></TD>
<TD><SELECT id="diameterSelect" onchange="tw_calc()" name="diameterSelect">
<OPTION value="eng" selected>feet</OPTION>
<OPTION value="met">meters</OPTION>
</SELECT></TD>
</TR>
</TABLE>
</P>

<!--#include file="ad_big_text_block.html" -->


<!--#include file="footer.html" -->

Offline paulstreats

  • Supreme Robot
  • *****
  • Posts: 1,381
  • Helpful? 21
Re: javascript NaN debugging problem
« Reply #1 on: October 20, 2007, 03:03:04 PM »
Nan sstands for Not A Number (if i remember correctly).

Ive playedwith values in the boxes on the link, it seems that anything with a decimal place in the 'desired acceleration' will create NaN to appear in the RMF box.

this seems to work

RMF=mass*((acceleration+gravity)*Math.sin(angle*Math.PI/180))*velocity*(1/efficiency)/(2*Math.PI);

the extra brackets around acceleration+gravity.

if you alert(acceleration+gravity*Math.sin(angle*Math.PI/180)); you get a silly output like 12.56.453456 note the two decimal places. I think that maybe the script was trying to just put the acceleration and gravity figures together rather than add them up (i think its the strange variables it was treating them as text and not numbers until a definate pre sum is performed on them)


The cookieObject() object hasnt been defined, maybe you missed the object script out?

Is this the first time of using cookies? (apart from the forum login)

« Last Edit: October 20, 2007, 03:11:36 PM by paulstreats »

Offline AdminTopic starter

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,658
  • Helpful? 169
    • Society of Robots
Re: javascript NaN debugging problem
« Reply #2 on: October 20, 2007, 03:17:21 PM »
thanks paulstreets!

your modification itself wouldn't work because it changes the equation, but it brought the problem to my attention

apparently numberA + numberB is completely different from parseFloat(numberA) + parseFloat(numberB)

lame javascript . . .

so this worked:
Code: [Select]
var RMF=mass*(parseFloat(acceleration)+parseFloat(gravity*Math.sin(angle*Math.PI/180)))*velocity*(1/efficiency)/(2*Math.PI)/wheels;
still working on that cookie bug . . .

Offline paulstreats

  • Supreme Robot
  • *****
  • Posts: 1,381
  • Helpful? 21
Re: javascript NaN debugging problem
« Reply #3 on: October 20, 2007, 03:22:47 PM »
I thought it might alter the equation a bit (javascript doesnt know exactly what type of var to use unless you predefine it somehow - it could be a char,string, int etc..)

You seem to be missing a predefined javascript object called cookieObject out of your script.

 I usually just use something like document.cookie = <object+value>

 retrieving them isnt usually that simple though

Offline paulstreats

  • Supreme Robot
  • *****
  • Posts: 1,381
  • Helpful? 21
Re: javascript NaN debugging problem
« Reply #4 on: October 20, 2007, 04:21:31 PM »
Ive altered one of my cookie object scripts if you would like it.

You can just copy this and paste it into your javascript code. The only change needed in your javascript is to change the ;

userCookie  = new cookieObject("tw_form", 365, "/", "tw_text1", "tw_text2", "tw_text3", "tw_text4", "tw_text5", "tw_text6", "tw_sel1", "tw_sel2", "tw_sel3", "tw_sel4")

to:

userCookie = new cookieObject()
      

//////////////////////
usage:


example:

userCookie = new cookieObject()

userCookie.put(#NAME,#VALUE,<optional #EXPIRYTIME>)
//use decimal value for expiry time such as 10 (will save the cookie for ten days) 1 = 1 day
//if no expiry time is used, the cookie stores with a pre - defined expiry of Time of 182 days
//its probably best to leave this out

userCookie.get(#name)

userCookie.erase(#name)




////////////////////
Code: [Select]
function cookieObject() {
this.cookie = null
this.cookies = new Array()
}

cookieObject.prototype.put = function(name, value, time) {
this.cookie = name + "=" + value
var date = new Date()
date.setTime(date.getTime()+(182*86400000))
if (time != null) {
date.setTime(date.getTime() + (days*86400000))
this.cookie += "; expires=" + date.toGMTString()
}//end of (time!=null)
this.cookie += "; path=/"
document.cookie = this.cookie
}//end of prototype put

cookieObject.prototype.get = function(name) {
name += "="
this.cookies = document.cookie.split(";")
for (var i = 0; i < this.cookies.length; i++) {
     this.cookie = this.cookies[i]
     while (this.cookie.charAt(0) == " ") {
     this.cookie = this.cookie.substring(1, this.cookie.length)
     } //end of while(1,this.cookie ..)
     if (this.cookie.indexOf(name) == 0) {
     return this.cookie.substring(name.length, this.cookie.length)
     }//end of if(this.cookie.indexOf)
}//end of for(var i = 0)
return null
}//end of prototype get


cookieObject.prototype.erase = function(name) {
this.save(name, "", -1)
}//end of prototype delete

« Last Edit: October 20, 2007, 05:46:14 PM by paulstreats »

Offline AdminTopic starter

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,658
  • Helpful? 169
    • Society of Robots
Re: javascript NaN debugging problem
« Reply #5 on: October 21, 2007, 09:21:15 AM »
hmmmm so I already have all that here:
http://www.societyofrobots.com/js/pcpcookielib.js

Im calling it at the top of my page:
Code: [Select]
<script language="javascript" src="http://www.societyofrobots.com/js/pcpcookielib.js"></script>
The error it gives says this:
Quote
Line: 193
Char:4
Error:'document.getElementByld(...)' is null or not an object
Code 0


But I think the troubled line number is wrong . . .

I got it to work on this test page:
http://www.societyofrobots.com/stuff.html
But not sure what Im doing different for the RMF one to cause it to break . . .

Offline paulstreats

  • Supreme Robot
  • *****
  • Posts: 1,381
  • Helpful? 21
Re: javascript NaN debugging problem
« Reply #6 on: October 21, 2007, 11:00:15 AM »
i cant see an effText form element any where?

Offline AdminTopic starter

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,658
  • Helpful? 169
    • Society of Robots
Re: javascript NaN debugging problem
« Reply #7 on: October 21, 2007, 11:20:38 AM »
this?
Code: [Select]
<TR>
 <TD>Expected Efficiency</TD>
 <TD><INPUT id="effText" onkeyup="tw_calc()" type="text" size="8" value="75" name="effText"></TD>
 <TD>%</TD>
</TR>

Offline AdminTopic starter

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,658
  • Helpful? 169
    • Society of Robots
Re: javascript NaN debugging problem
« Reply #8 on: October 21, 2007, 02:31:07 PM »
Ok I fixed one of the errors . . . apparently you need to list the javascript code AFTER the form code . . . argggg.

But I got one last error - upon exiting the page it gives me a warning, and the cookie doesnt save.

Thoughts?

Offline AdminTopic starter

  • Administrator
  • Supreme Robot
  • *****
  • Posts: 11,658
  • Helpful? 169
    • Society of Robots
Re: javascript NaN debugging problem
« Reply #9 on: October 21, 2007, 02:46:37 PM »
I finally figured out the last bug!! Yaaaay! (man I suck at programming)

I wrote rotText instead of diameterText because I forgot to carry over changes I made . . .

 


Get Your Ad Here

data_list