JavaScript is making my brain hurt. [Sorted]

For help and advice about coding

Moderator: Tech Moderators

User avatar
Tinlad
Tech Moderator
Posts: 3345
Joined: Sun Mar 18, 2007 4:54 pm
Tag: Dog food scientist.
Location: Leeds
Contact:

JavaScript is making my brain hurt. [Sorted]

Postby Tinlad » Mon Apr 05, 2010 4:13 pm

I'm currently abusing JavaScript horribly and making it do some physics.

My code is something like this:

Code: Select all

particle = new Particle(x,y,mass);
tree = new BHTree();

tree.addParticle(particle);


This works. However, I want lots of particles. So I did this:

Code: Select all

particles = new Array();
tree = new BHTree();

particles[0] = new Particle(x1,y1,m1);
particles[1] = new Particle(x2,y2,m2);
particles[2] = new Particle(x3,y3,m3);

for(i = 0; i < particles.length; i++){
    tree.addParticle(particles[i]);
}


This does not work. Confusingly (for me), this does work:

Code: Select all

particles = new Array();
tree = new BHTree();

particles[0] = new Particle(x1,y1,m1);
particles[1] = new Particle(x2,y2,m2);
particles[2] = new Particle(x3,y3,m3);

tree.addParticle(particles[0]);
tree.addParticle(particles[1]);
tree.addParticle(particles[2]);


What's going on? I'm guessing that one method passes the object itself, and one passes a copy of it*, but if one of you wizards could shed some light on this then I'd be very grateful. :wubs:

* My OO programming skills are very limited, as is my knowledge of the correct terminology.

User avatar
nomis
Site Admin
Posts: 1412
Joined: Sun Mar 18, 2007 4:50 pm
Tag: Hasn't realised that there are custom ranks yet
Contact:

Re: JavaScript is making my brain hurt.

Postby nomis » Mon Apr 05, 2010 4:39 pm

Rule #1 of JavaScript is "everything is a reference", so nope, it's not a copy vs reference thing.

What do you get if you alert what you expect to be the object in the loop?

Why not do the instantiation and addParticle call in the same loop?

What does firebug say? Do you get any errors?

Long shot, but I wonder if its because you've not actually declared particles as an array/object.

Does doing this make any difference:

Code: Select all

var particles = [];

particles[particles.length] = new particle();
particles[particles.length] = new particle();
particles[particles.length] = new particle();

User avatar
Tinlad
Tech Moderator
Posts: 3345
Joined: Sun Mar 18, 2007 4:54 pm
Tag: Dog food scientist.
Location: Leeds
Contact:

Re: JavaScript is making my brain hurt.

Postby Tinlad » Mon Apr 05, 2010 7:56 pm

Cheers Si. Don't know why I didn't notice this before:

Code: Select all

foo.addParticle(p[0]);
foo.addParticle(p[1]);
foo.addParticle(p[2]);
foo.addParticle(p[3]);

alert(foo.count);

Alerts "4", which is correct.

Code: Select all

for(i = 0; i < p.length; i++){
    foo.addParticle(p[i]);
}

alert(foo.count);

Alerts "2", which is wrong. Closer inspection of foo shows that only p[0] and p[1] have been added, with p[2] and p[3] being ignored completely. Switching p.length to 4 makes no difference.

Bah. This is sorcery. I'm going to have to come back to this tomorrow, because it's making no sense to me at all at the moment...

User avatar
Tinlad
Tech Moderator
Posts: 3345
Joined: Sun Mar 18, 2007 4:54 pm
Tag: Dog food scientist.
Location: Leeds
Contact:

Re: JavaScript is making my brain hurt.

Postby Tinlad » Tue Apr 06, 2010 2:15 pm

Solved.

I'm a massive n00b. The addParticle() method uses a for loop, and I'd done this:

Code: Select all

for(i = 0; i < bar; i++){
    //stuff
}

...instead of this:

Code: Select all

for(var i = 0; i < bar; i++){
    //stuff
}


... so when I also used i in the loop that calls addParticle(), the same variable was used for both, resulting in only half the array being added. :oops:

User avatar
doogle
Posts: 783
Joined: Wed Mar 21, 2007 1:25 am
Tag: Likes squeezy cheesy cobs.
Location: Sherwood Forest
Contact:

Re: JavaScript is making my brain hurt. [Sorted]

Postby doogle » Tue May 18, 2010 9:20 am

Whenever I've used for loops in my javascripts, I always set i=0 beforehand, just in case I get that kind of problem, either that or use a different character for each loop.

User avatar
nomis
Site Admin
Posts: 1412
Joined: Sun Mar 18, 2007 4:50 pm
Tag: Hasn't realised that there are custom ranks yet
Contact:

Re: JavaScript is making my brain hurt. [Sorted]

Postby nomis » Tue May 18, 2010 8:24 pm

doogle wrote:Whenever I've used for loops in my javascripts, I always set i=0 beforehand, just in case I get that kind of problem, either that or use a different character for each loop.


Code: Select all

i = 0;
for(i=0; i< x; i++){


is no different from

Code: Select all

for(i=0; i< x; i++){


I didn't see Tinlad's code but from his description it sounds like your solution, at best, would have reset the outer loop's i to 0 on every iteration of the inner loop. AKA an infinite loop, so not sure that would have helped.

Tinlad's problem was a scope issue, which he solved by using var on the inner loop (and thus forcing the method to use a local rather than global i).

User avatar
doogle
Posts: 783
Joined: Wed Mar 21, 2007 1:25 am
Tag: Likes squeezy cheesy cobs.
Location: Sherwood Forest
Contact:

Re: JavaScript is making my brain hurt. [Sorted]

Postby doogle » Thu Jun 24, 2010 10:27 am

Ah, I missed the bit about it already being inside a loop :oops:


Return to “Coding”

Who is online

Users browsing this forum: No registered users and 0 guests

cron