How to put an array in an object?

Discussion in 'Javascript' started by Kokolums, Apr 23, 2004.

  1. Kokolums

    Kokolums Guest

    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.
     
    Kokolums, Apr 23, 2004
    #1
    1. Advertising

  2. Kokolums

    Lee Guest

    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);
    }
     
    Lee, Apr 23, 2004
    #2
    1. Advertising

  3. Kokolums

    Kokolums Guest

    Lee <> wrote in message news:<>...

    >
    > 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.
     
    Kokolums, Apr 24, 2004
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Haydnw
    Replies:
    4
    Views:
    3,618
    Haydnw
    Aug 16, 2004
  2. Peter Afonin

    How to put an array into a table

    Peter Afonin, Sep 8, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    378
    Peter Afonin
    Sep 9, 2004
  3. Replies:
    9
    Views:
    579
    justanotherguy
    Dec 3, 2004
  4. Gabriel Rossetti
    Replies:
    3
    Views:
    577
    Jerry Hill
    Apr 25, 2008
  5. Richard Lionheart
    Replies:
    27
    Views:
    424
    Jean-Hugues ROBERT
    May 4, 2004
Loading...

Share This Page