PDA

View Full Version : Java Script Populating 2D array thought prompt???



JackieBolinsky
01-24-2012, 09:38 PM
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:

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:

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 ?

JackieBolinsky
01-25-2012, 06:38 AM
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

_________________
Javascript Codes (http://www.javascriptbank.com)

miketaylor
02-05-2012, 01:05 PM
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!"

*******************
Toronto Lofts (http://www.lofthunting.ca/)