Javascript problem. Probably newb error but I just don't get it!

usefuldesign.au's picture

I'm writing a simple JS routine to filter a structure of points into those points that are within a prescribed distance of a particular point, which is described by it's index value in the point structure.

So it's not the first time JS has refused to pull nested values even when I'm using code I've used successfully elsewhere ie. [i] ["X"]. And it's not for the first time it refuses to parse a line like:

 total_points = all_points.length;

even though the exact same line works in another patch parsing the same structure data. (so I've used the structure count patch and pull it in ;) )

But this really gets me, as a way of debugging I'm logging some point co-ords (X,Y,Z) as __numbers out of the patch.

If I literally assign them as in result.X = 0.123 no worries but if I use the incredibly complicated code of

var x1 = new Number();
x1 = 2 + 3;
result.X = x1;

I get told Argument X: Incorrect type, should be a number ( :\ x100 )

If anybody can explain this to me I'd be enormously grateful. Shutting down in frustration.

Cheers Alastair

PreviewAttachmentSize
Filter by distance.zip15.97 KB

bbinkovitz's picture
Re: Javascript problem. Probably newb error but I just ...

I'm not sure exactly where in your code this error is happening because you don't appear to actually have this snippet of code in your javascript. However I did notice you had a line commented out with a comment that indicated frustration because it wasn't working.

//   var totalPoints = points.length;  // WHy the f##k doesn't this work. It works 
in a similar JS patch. QC JS is flacky I'm sure of it

The problem with this is not the line of code itself; it's the fact that it's not included in the stipulation if (!_testMode){ }. It performs the "length" method on the "points" structure. In test mode, all inputs and outputs are set to null. If you try to perform a method on a null structure, it gets all offended.

It's possible you were getting a cascade effect and the error you mentioned above was caused by something related to this? Just speculating since I'm not sure what you tried to do.

usefuldesign.au's picture
Re: Javascript problem. Probably newb error but I just ...

Thanks for the explanation on the .length method, bbinkovitz. I'll use it inside the (!_testMode) from now on and hopefully that's one problem sorted! Thanks so much for explaining that, I was speculating if it had something to do with initial conditions but didn't make the connection (duh).

As it was commented out I guess I wasn't effecting other code/interpreting but I have seen parse errors highlighted on commented out lines before.

I solved some of this problem with a work around, multiply numbers keyed into structures by one (x1.). It seems casting from one type to another (eg a keyed item like ["X"] inside a structure to a number) doesn't always work. While adding a number to another didn't work, multiplty did. Possibly because add operator can work with strings also — I've no idea.

I'm am convinced JS patches can go 'off' in the interpreter and not come back. I've found if I retype code into a new patch, making sure each line parses okay before typing another I get the same code to work where in another patch that Iv'e been mucking around in for ages wont work. So when I'm retyping a loop, I type the conditions, then both braces, then I fill in the code I wish to iterate. Parsing each line as I go. This seems to be the only way I can rescue a patch sometimes.

Side Issue: I also noticed that inside nested loops, you can't use the parent loop variable (say 'i') in the looping increment statement of the child loop (say for (j=0; j<24; j = j + 24/i). It crashes every time. Another variable dependant on 'i' used in the increment will crash it. Not that it's very common to need to do this, I know…

If I hadn't worked out how to coerce types I was going to learn the Lua scripting language I actually had a read of some of the literature then came back to JS for one last try!

Thanks again for responding. My javascript questions always go unanswered here an general JS Forums aren't relevant sometimes as the issue can be QC specific like the (!_testMode) is. I think it's like I didn't study Comp Sci for 3+ years to debug so other guys code… oh well I try to help those even less competent than me.

usefuldesign.au's picture
Re: Javascript problem. Probably newb error but I just ...

bbinkovitz wrote:
I'm not sure exactly where in your code this error is happening because you don't appear to actually have this snippet of code in your javascript. However I did notice you had a line commented out with a comment that indicated frustration because it wasn't working.

Yeah okay I was trying so many combinations… change from result.X = 2345; to result.X = x; and I get the error.

You are correct. I was declaring result.X outside the (!_testMode) braces and the definition was inside the braces. Thanks again. I hope this has been the source of all my wasted time — not getting tooo hopefully.