www.webdeveloper.com
Results 1 to 5 of 5

Thread: Problem with arrays

Hybrid View

  1. #1
    Join Date
    Nov 2013
    Posts
    2

    Problem with arrays

    Hi, I'm trying to create a function that saves a vector that represents time, or several instances of time in this case, and for each instance it holds a vector of several positions (x,y) of several objects. For some reason when I run it the first alert gives me the results as I want them but then of the last alerts it just gives me same result (on the 3) and is the last one that run through the while. It's like it's only saving the last entry. What am I doing wrong? Thank you and sorry for my bad English.

    function randomNumbers(){
    i=0;
    while (i<=tRight-1){
    w=0;
    while(w<=Numb_lights-1){
    x=Math.round(Math.random()*5)*100+307;
    y=Math.round(Math.random()*2)*150+95;
    valoresx[w]=x;
    valoresy[w]=y;
    w=w+1;
    }
    valores[0]=valoresx;
    valores[1]=valoresy;
    valorest=new Array();
    valorest=valores;
    alert(valorest
    );
    i=i+1;
    }
    alert(valorest[0][0]);
    alert(valorest[1][0]);
    alert(valorest[2][0]);
    alert(valorest[3][0]);
    }

  2. #2
    Join Date
    Oct 2012
    Location
    Croatia
    Posts
    255
    First, put your code inside of the code tag. Second, it would be helpful if you post the entire code.

  3. #3
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,506
    If you have an array 'x' and you need to add a further dimension to it,
    Code:
    c = 0;
    x = new Array();
    x[c] = new Array();
    now if you increment the value of c in a loop, you will get x[n] arrays that are arrays in themselves.
    Code:
    x = new Array();
    for( c = 0; c<10; c++){
        x[c] = new Array();
        }
    So storing a value at a vector of 3,9 for example would be as simple as ...
    Code:
    x[3][9] = 1;
    and if you needed to store a pair of numbers, you could either use an array or an object.

    Array method...
    Code:
    x[3][9] = new Array(1,2);
    Object method
    Code:
    x[3][9] = {xvalue:1,yvalue:2};
    the advantage of an object method is you can name the elements and use the names of those elements like this
    Code:
    yval = x[3][9].yvalue;
    in the array method you would need to know that x[3][9][1] was the y value or use associative array names like in an object like
    Code:
    x[3][9] = new Array();
    x[3][9]['xvalue'] = 1;
    x[3][9]['yvalue'] = 2;
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

  4. #4
    Join Date
    Nov 2013
    Posts
    2
    Thank you for your answers! I just did this:

    Code:
    x_image = new Array();
    	for( c = 0; c<3; c++){
        	x_image[c] = new Array();
        }
    	for( cc = 0; cc<25; cc++){
    		valor_x=660+cc*154;
    		for( ccc = 0; ccc<3; ccc++){
    			valor_y=914+ccc*154;
        	                x_image[cc][ccc] = new Array();
    		        x_image[cc][ccc][0] = valor_x;
    		        x_image[cc][ccc][1] = valor_y;
    		}
        }
    but it is not working, it says "Cannot set property '0' of undefined " on the "x_image[cc][ccc] = new Array();" line. What I am doing wrong? O.o

  5. #5
    Join Date
    Mar 2007
    Location
    localhost
    Posts
    2,506
    You need to have something like this.

    x_image = new Array();
    for( c = 0; c<3; c++){
    x_image[c] = new Array();
    }
    for( cc = 0; cc<25; cc++){
    x_image[cc] = new Array();
    valor_x=660+cc*154;
    for( ccc = 0; ccc<3; ccc++){
    valor_y=914+ccc*154;
    x_image[cc][ccc] = new Array();
    x_image[cc][ccc][0] = valor_x;
    x_image[cc][ccc][1] = valor_y;
    }
    }
    Last edited by \\.\; 12-05-2013 at 10:48 AM.
    Yes, I know I'm about as subtle as being hit by a bus..(\\.\ Aug08)
    Yep... I say it like I see it, even if it is like a baseball bat in the nutz... (\\.\ Aug08)
    I want to leave this world the same way I came into it, Screaming, Incontinent & No memory!
    I laughed that hard I burst my colostomy bag... (\\.\ May03)
    Life for some is like a car accident... Mine is like a motorway pile up...

    Problems with Vista? :: Getting Cryptic wid it. :: The 'C' word! :: Whois?

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles