Help with Javascript array

Need help with an engine or coding not on the list? Need help with a game or the website and forums here? Direct all questions here.
Post Reply
User avatar
Script47
Posts: 147
Joined: Thu Nov 21, 2013 6:11 pm

Help with Javascript array

Post 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
User avatar
a_bertrand
Posts: 1537
Joined: Mon Feb 25, 2013 1:46 pm

Re: Help with Javascript array

Post 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.
Creator of Dot World Maker
Mad programmer and annoying composer
User avatar
Jackolantern
Posts: 10893
Joined: Wed Jul 01, 2009 11:00 pm

Re: Help with Javascript array

Post 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.
The indelible lord of tl;dr
User avatar
hallsofvallhalla
Site Admin
Posts: 12031
Joined: Wed Apr 22, 2009 11:29 pm

Re: Help with Javascript array

Post by hallsofvallhalla »

yeah thats odd unless the ++ is pushing instead of adding.
User avatar
MikuzA
Posts: 395
Joined: Thu Aug 08, 2013 8:57 am

Re: Help with Javascript array

Post 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++;
Why so serious?

Business Intelligence, Data Engineering, Data Mining
PHP, HTML, JavaScript, Bash/KornShell, Python, C#, PL/SQL
MySQL, DB2, Oracle, Snowflake
Pentaho, DataStage, Matillion, Unity3D, Blender
User avatar
a_bertrand
Posts: 1537
Joined: Mon Feb 25, 2013 1:46 pm

Re: Help with Javascript array

Post by a_bertrand »

No if it is an array ;)
Creator of Dot World Maker
Mad programmer and annoying composer
User avatar
Script47
Posts: 147
Joined: Thu Nov 21, 2013 6:11 pm

Re: Help with Javascript array

Post 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. :)
User avatar
Jackolantern
Posts: 10893
Joined: Wed Jul 01, 2009 11:00 pm

Re: Help with Javascript array

Post 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 ;)
The indelible lord of tl;dr
User avatar
Script47
Posts: 147
Joined: Thu Nov 21, 2013 6:11 pm

Re: Help with Javascript array

Post 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. :)
User avatar
Jackolantern
Posts: 10893
Joined: Wed Jul 01, 2009 11:00 pm

Re: Help with Javascript array

Post 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?
The indelible lord of tl;dr
Post Reply

Return to “Advanced Help and Support”