How to put an array in an object?

K

Kokolums

I'm writing a little javascript program to help keep track voting logs
for a game of "werewolf".

I then tried to add an array to my object constructor and the code
went kablooey on me. I feel lost here.

//object constructor
function VoterRecord(name, angrymob, tally) {
this.name = name;
this.angrymob = angrymob;
this.tally = tally;

That constructor works if angrymob is just a simple variable, as shown
in the code snippet below:

i = 0;
village = new Array(50);
for (var i=0; i<49; i++)
village = new VoterRecord("","",0);

But angrymob needs to be an array filled with the names of people
voting to lynch the person. Its when I try that (as in the code
below) it goes kablooey:

village = new Array(50);
mob = new Array(20);
for (var i=0; i<49; i++)
village = new VoterRecord("",mob,0);

I really don't know the syntax here. How do you put an array in an
object? I'm lost.
 
L

Lee

Kokolums said:
I'm writing a little javascript program to help keep track voting logs
for a game of "werewolf".

I then tried to add an array to my object constructor and the code
went kablooey on me. I feel lost here.

//object constructor
function VoterRecord(name, angrymob, tally) {
this.name = name;
this.angrymob = angrymob;
this.tally = tally;

That constructor works if angrymob is just a simple variable, as shown
in the code snippet below:

i = 0;
village = new Array(50);
for (var i=0; i<49; i++)
village = new VoterRecord("","",0);

But angrymob needs to be an array filled with the names of people
voting to lynch the person. Its when I try that (as in the code
below) it goes kablooey:

village = new Array(50);
mob = new Array(20);
for (var i=0; i<49; i++)
village = new VoterRecord("",mob,0);

I really don't know the syntax here. How do you put an array in an
object? I'm lost.



In general, it would be easier to help you if you gave more
detail that "it goes kablooey".

In this case, one problem is that you're assigning the same
array to every new VoterRecord. I don't think that's what
you want. Also, if you want 50 elements in your array,
you want i to loop from 0 to 49, not 48. Or simply use the
size of the array, to be more general:

village = new Array(50);
for (var i=0; i<village.length; i++) {
mob = new Array(20);
village = new VoterRecord("",mob,0);
}
 
K

Kokolums

Lee said:
In general, it would be easier to help you if you gave more
detail that "it goes kablooey".

In this case, one problem is that you're assigning the same
array to every new VoterRecord. I don't think that's what
you want. Also, if you want 50 elements in your array,
you want i to loop from 0 to 49, not 48. Or simply use the
size of the array, to be more general:

village = new Array(50);
for (var i=0; i<village.length; i++) {
mob = new Array(20);
village = new VoterRecord("",mob,0);
}


That's it! Keep Defining a new array in a loop over and over! That
fixed everything! Works like a charm now. Much thanks sir.
 

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

Forum statistics

Threads
473,769
Messages
2,569,582
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top