server monitoring


Closed Thread
Results 1 to 3 of 3
  1. #1
    Join Date
    Jan 2012
    Posts
    5

    Talking Java Script Populating 2D array thought prompt???

    Hey I need to make a simple program which consists of insert function which adds the data asked for to 2D array.. I think i did the array but I am not right sure
    (data to be entered in name, height, weight,)

    Code:
    Code:
    var person = new Array(i)
    for (var h = 0; h < i; h++)
    person[i] = new Array(j);
    I know that one dimension is going to be 3 but not sure which is the horizontal the vertical has to be undefined since this will be like a small database and i dont know how much data will be added.

    also how do i make the insertion function ? could i do something like

    Code:
    Code:
    var createPerson =function(name,height,weight){
    var person = {}
    person.name = name
    person.height = height
    person.weight = weight
    return person
    };
    
    var person = {name: "James Bond",height: "0", weight: "0"};
    I am really confused at this stage.. not sure how to do this any ideas ?

  2. #2
    Join Date
    Jan 2012
    Posts
    5

    Lightbulb

    Hello there,

    Get a random value in between 1 and 100 (num1). Add it to the array. Find another random value from 1 to (100 - num1) (num2). Add it to the array. Etc. Continue until the array size is 4. The last element should be 100 - the sum of the 4 numbers. Add that array to the bigger array.

    Regards,
    Jackie

    _________________
    Last edited by DougN; 09-06-2012 at 03:47 PM.

  3. #3
    miketaylor Guest
    I just noticed too, when i was trying to implement a 2D array into JS. It is Screwed. But here is a solution if you want to stick to multidimensional arrays: An Object.

    function Array2D() {
    function setArray(length) { // set an array inside the object
    this.elem=new Array();
    for(i=0;i<length;i++) {
    this.elem[i]=null;
    }
    }
    this.setArray=setArray;
    this.elem=null; // our array
    }

    With this little fella you can hide your intentions from the Evil Javascript Developers, and secretly make a 2D array:

    arr = new Array();
    arr[0] = new Array2D();
    arr[0].setArray(2);
    arr[0].elem[0] = "Hy, ";
    arr[0].elem[1] = "I'm ";
    arr[1] = new Array2D();
    arr[1].setArray(2);
    arr[1].elem[0] = "Da";
    arr[1].elem[1] = "vid!";

    alert(arr[0].elem[0] + arr[0].elem[1] + arr[1].elem[0] + arr[1].elem[1]); // this will say "Hy, I'm David!"

    *******************
    Last edited by DougN; 09-06-2012 at 03:47 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts