Page 1 of 1

Help with Javascript array

Posted: Mon Jan 20, 2014 6:46 pm
by Script47
I have an Javascript array object:

Code: Select all

Game.UsersItems = {itemAutoClickers: [0], itemClickingTeam: [0]};
Which stores the users items and if they buy an "itemAutoClickers" then that value will increment with the following code:

Code: Select all

Game.UsersItems.itemAutoClickers[0]++;
However all this works but that specific item (itemAutoClickers) should give the user 1 score each time. but for some reason it gives them 2. I decided to do the following:

Code: Select all

console.log(Game.UsersItems.itemAutoClickers);
It displays:
[0]
[0]
So I was wondering if you have any ideas why the code won't work as intended?

Just to show you the log output:
Error PC.png

Re: Help with Javascript array

Posted: Mon Jan 20, 2014 6:59 pm
by a_bertrand
Why don't you use the JS debugger included in any of the major browsers? Press F12 => and you have the developer tools, then watch your script, put a breakpoint and check your variables via the watches.

Re: Help with Javascript array

Posted: Mon Jan 20, 2014 7:03 pm
by Jackolantern
Can you post the whole script it is being used in? I can't duplicate either of those issues with the code posted.

Re: Help with Javascript array

Posted: Mon Jan 20, 2014 7:16 pm
by hallsofvallhalla
yeah thats odd unless the ++ is pushing instead of adding.

Re: Help with Javascript array

Posted: Tue Jan 21, 2014 5:12 am
by MikuzA
Not an Javascript expert but isn't this wrong>

Code: Select all

Game.UsersItems.itemAutoClickers[0]++;
Shouldn't it be,

Code: Select all

Game.UsersItems.itemAutoClickers++;

Re: Help with Javascript array

Posted: Tue Jan 21, 2014 7:13 am
by a_bertrand
No if it is an array ;)

Re: Help with Javascript array

Posted: Tue Jan 21, 2014 5:09 pm
by Script47
hallsofvallhalla wrote:yeah thats odd unless the ++ is pushing instead of adding.
I will try put:

Code: Select all

Blah = Blah +1
Thanks everyone for all the help. :)

Re: Help with Javascript array

Posted: Wed Jan 22, 2014 12:36 am
by Jackolantern
Script47 wrote:
hallsofvallhalla wrote:yeah thats odd unless the ++ is pushing instead of adding.
I will try put:

Code: Select all

Blah = Blah +1
Thanks everyone for all the help. :)
There is no difference between

Code: Select all

a++;
and

Code: Select all

a = a + 1;
Most JS engines substitute the latter for the former. If you are still having issues, post the whole script so we can see where the issue is ;)

Re: Help with Javascript array

Posted: Wed Jan 22, 2014 3:51 pm
by Script47
Jackolantern wrote:
Script47 wrote:
hallsofvallhalla wrote:yeah thats odd unless the ++ is pushing instead of adding.
I will try put:

Code: Select all

Blah = Blah +1
Thanks everyone for all the help. :)
There is no difference between

Code: Select all

a++;
and

Code: Select all

a = a + 1;
Most JS engines substitute the latter for the former. If you are still having issues, post the whole script so we can see where the issue is ;)
It actually worked when I changed it to that. :)

Re: Help with Javascript array

Posted: Wed Jan 22, 2014 9:35 pm
by Jackolantern
You have an error elsewhere in your code then, and it should be considered unstable. There is no difference between a++ and a = a + 1. Was that the only change you made, and it started working?