undefined NaN, surely not!!

O

oliver hadfield

Hello all

I am having a small problem wih a javascript, could anyone help me
please

1. first of all

this code requires a form f01 and textarea t01

<form name ="f01">
<textarea name="t01" rows="80" cols="50">
</textarea></form>

2. the javascript is as follows:
----------------------

function point(x,y,z){
this.x = x;
this.y = y;
this.z = z;
}
function mkPoints(){
tmp01 = new point(-50,50,-50);
tmp02 = new point(50,50,-50);
tmp03 = new point(-50,-50,-50);
tmp04 = new point(50,-50,-50);
tmp05 = new point(-50,50,50);
tmp06 = new point(50,50,50);
tmp07 = new point( -50,-50,50);
tmp08 = new point( 50, -50 ,50 );
}
function rotate(xrot,yrot){
for (i=1;i<=8;i++){
document.all["tmp_zpos0"+i] = (eval("tmp0"+i+".z") * Math.cos(yrot))
- (eval("tmp0"+i+".x") * Math.sin(yrot))
document.all["tmp0"+i+".x"] =
(eval("tmp0"+i+".z")*Math.sin(yrot))+(eval("tmp0"+i+".x")*Math.cos(yrot));
document.all["tmp0"+i+".z"] =
(eval("tmp0"+i+".y")*Math.sin(xrot))+(eval("document.all.tmp_zpos0"+i)*Math.cos(xrot));
document.all["tmp0"+i+".y"] =
(eval("tmp0"+i+".y")*Math.cos(xrot))-(document.all["tmp_zpos0"+i]*Math.sin(xrot));
document.f01.t01.value = document.f01.t01.value +
document.all["tmp0"+i+".y"] + "\n"
}
setTimeout("rotate(0.02,0.01)", 1000);
}
function runAll (){
mkPoints()
rotate(0.02,0.01)
}
--------

The problem is as follows - I am baffled as to why it appears to retun
Nan or undefined when the values are clearly are there.

Please help,
Thank you
oliverh
 
L

Lasse Reichstein Nielsen

1. first of all

this code requires a form f01 and textarea t01

<form name ="f01">
<textarea name="t01" rows="80" cols="50">
</textarea></form>

2. the javascript is as follows:
----------------------

function point(x,y,z){
this.x = x;
this.y = y;
this.z = z;
}
function mkPoints(){
tmp01 = new point(-50,50,-50);
tmp02 = new point(50,50,-50);
tmp03 = new point(-50,-50,-50);
tmp04 = new point(50,-50,-50);
tmp05 = new point(-50,50,50);
tmp06 = new point(50,50,50);
tmp07 = new point( -50,-50,50);
tmp08 = new point( 50, -50 ,50 );

I would make an array of these points instead of eight global variables.
function rotate(xrot,yrot){
for (i=1;i<=8;i++){
document.all["tmp_zpos0"+i] = (eval("tmp0"+i+".z") * Math.cos(yrot))
- (eval("tmp0"+i+".x") * Math.sin(yrot))

You never need eval for things like this. It is even much faster to do
it without.

To access the global variabel "tmp0"+i, you can just write
window["tmp0"+i]

You use document.all, which is not as widely applicable as
document.getElementById. I think you just wanted to use "eval" anyway.
You assign directly to the result of document.all[...]. That is most
likely not what you want.

You don't mention an HTML element called "tmp_zpos01", so I guess you
meant to write
eval("tmp_zpos0"+i) = ...
where tmp_zpos01 is a variable?
In that case, you only need one variable, not one for each point.

I would write this line as

var tmp_zpos = window["tmp0"+i].z * Math.cos(yrot) -
window["tmp0"+i].x * Math.sin(yrot);
document.all["tmp0"+i+".x"] =
(eval("tmp0"+i+".z")*Math.sin(yrot))+(eval("tmp0"+i+".x")*Math.cos(yrot));

window["tmp0"+i].x = window["tmp0"+i].z*Math.sin(yrot) +
window["tmp0"+i].x*Math.cos(yrot);
document.all["tmp0"+i+".z"] =
(eval("tmp0"+i+".y")*Math.sin(xrot))+(eval("document.all.tmp_zpos0"+i)*Math.cos(xrot));

window["tmp0"+i].z = window["tmp0"+i].y*Math.sin(xrot) +
tmp_zpos*Math.cos(xrot);
document.all["tmp0"+i+".y"] =
(eval("tmp0"+i+".y")*Math.cos(xrot))-(document.all["tmp_zpos0"+i]*Math.sin(xrot));

window["tmp0"+i].y = window["tmp0"+i].y*Math.cos(xrot) +
tmp_zpos*Math.sin(xrot);
document.f01.t01.value = document.f01.t01.value +
document.all["tmp0"+i+".y"] + "\n"

Here you use document.all again, where you definitly just meant to use
eval (and you shouldn't use eval either).

document.forms['f01'].elements['t01'].value += window["tmp0"+i].y+"\n";

The problem is as follows - I am baffled as to why it appears to retun
Nan or undefined

*What* returns NaN or undefined? There are not return statements in
the code. I guess it is one of the document.all's that return undefined
because you ask for an element that isn't there.
when the values are clearly are there.

Are they. Have you tested that the values are correct (e.g. by inserting
alerts or using a debugger)?

/L
 
L

Lee

oliver hadfield said:
Hello all

I am having a small problem wih a javascript, could anyone help me
please

1. first of all

this code requires a form f01 and textarea t01

<form name ="f01">
<textarea name="t01" rows="80" cols="50">
</textarea></form>

2. the javascript is as follows:
----------------------

function point(x,y,z){
this.x = x;
this.y = y;
this.z = z;
}
function mkPoints(){
tmp01 = new point(-50,50,-50);
tmp02 = new point(50,50,-50);
tmp03 = new point(-50,-50,-50);
tmp04 = new point(50,-50,-50);
tmp05 = new point(-50,50,50);
tmp06 = new point(50,50,50);
tmp07 = new point( -50,-50,50);
tmp08 = new point( 50, -50 ,50 );
}
function rotate(xrot,yrot){
for (i=1;i<=8;i++){
document.all["tmp_zpos0"+i] = (eval("tmp0"+i+".z") * Math.cos(yrot))
- (eval("tmp0"+i+".x") * Math.sin(yrot))
document.all["tmp0"+i+".x"] =
(eval("tmp0"+i+".z")*Math.sin(yrot))+(eval("tmp0"+i+".x")*Math.cos(yrot));
document.all["tmp0"+i+".z"] =
(eval("tmp0"+i+".y")*Math.sin(xrot))+(eval("document.all.tmp_zpos0"+i)*Math.cos(xrot));
document.all["tmp0"+i+".y"] =
(eval("tmp0"+i+".y")*Math.cos(xrot))-(document.all["tmp_zpos0"+i]*Math.sin(xrot));
document.f01.t01.value = document.f01.t01.value +
document.all["tmp0"+i+".y"] + "\n"
}
setTimeout("rotate(0.02,0.01)", 1000);
}
function runAll (){
mkPoints()
rotate(0.02,0.01)
}

What returns NaN? None of the functions you've shown us return anything.
You're using document.all, which is obsolete, and using eval() where it
is not necessary, which is not a good practice.
 
O

oliverh

Lasse Reichstein Nielsen said:
(e-mail address removed) (oliver hadfield) writes:

Hey Lasse,
Thank you very much for looking into the problem in such depth.

I would make an array of these points instead of eight global variables.

An array. I will have to look into how to do that.
function rotate(xrot,yrot){
for (i=1;i<=8;i++){
document.all["tmp_zpos0"+i] = (eval("tmp0"+i+".z") * Math.cos(yrot))
- (eval("tmp0"+i+".x") * Math.sin(yrot))

You never need eval for things like this. It is even much faster to do
it without.

To access the global variabel "tmp0"+i, you can just write
window["tmp0"+i]

You use document.all, which is not as widely applicable as
document.getElementById. I think you just wanted to use "eval" anyway.
You assign directly to the result of document.all[...]. That is most
likely not what you want.

You don't mention an HTML element called "tmp_zpos01", so I guess you
meant to write
eval("tmp_zpos0"+i) = ...
where tmp_zpos01 is a variable?
In that case, you only need one variable, not one for each point.

Good advice, I cut out the eval("tmp_zpos0"+i)
so I re-wrote the following:

window.tmpZpos = (window["tmp0"+i].z * Math.cos(yrot)) -
(window["tmp0"+i].x * Math.sin(yrot));
window["tmp0"+i+".x"] = (window["tmp0"+i].z * Math.sin(yrot)) +
(window["tmp0"+i].x * Math.cos(yrot));
window["tmp0"+i+".z"] = (window["tmp0"+i].y * Math.sin(xrot)) +
(window.tmpZpos * Math.cos(xrot));
window["tmp0"+i+".y"] = (window["tmp0"+i].y * Math.cos(xrot)) -
(window.tmpZpos * Math.sin(xrot));

works perfectly
I would write this line as

var tmp_zpos = window["tmp0"+i].z * Math.cos(yrot) -
window["tmp0"+i].x * Math.sin(yrot);
document.all["tmp0"+i+".x"] =
(eval("tmp0"+i+".z")*Math.sin(yrot))+(eval("tmp0"+i+".x")*Math.cos(yrot));

window["tmp0"+i].x = window["tmp0"+i].z*Math.sin(yrot) +
window["tmp0"+i].x*Math.cos(yrot);
document.all["tmp0"+i+".z"] =
(eval("tmp0"+i+".y")*Math.sin(xrot))+(eval("document.all.tmp_zpos0"+i)*Math.cos(xrot));

window["tmp0"+i].z = window["tmp0"+i].y*Math.sin(xrot) +
tmp_zpos*Math.cos(xrot);
document.all["tmp0"+i+".y"] =
(eval("tmp0"+i+".y")*Math.cos(xrot))-(document.all["tmp_zpos0"+i]*Math.sin(xrot));

window["tmp0"+i].y = window["tmp0"+i].y*Math.cos(xrot) +
tmp_zpos*Math.sin(xrot);
document.f01.t01.value = document.f01.t01.value +
document.all["tmp0"+i+".y"] + "\n"

Here you use document.all again, where you definitly just meant to use
eval (and you shouldn't use eval either).

document.forms['f01'].elements['t01'].value += window["tmp0"+i].y+"\n";

The problem is as follows - I am baffled as to why it appears to retun
Nan or undefined

*What* returns NaN or undefined? There are not return statements in
the code. I guess it is one of the document.all's that return undefined
because you ask for an element that isn't there.
when the values are clearly are there.

Are they. Have you tested that the values are correct (e.g. by inserting
alerts or using a debugger)?

/L

I was using the textarea t01 to return the results.

Thank you again,

oliverh
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,234
Latest member
SkyeWeems

Latest Threads

Top