views:

903

answers:

3

Here is a sample of the global.heights array:

[[190, 76, 209, 57, 114, 171, 76, 513, 209, 171, 76, 152, 76, 76, 190, 114, 95, 76, 76, 95, 57, 133, 114], [152, 76, 133, 38, 95, 133, 76, 342, 190, 114, 57, 152, 76, 57, 133, 76, 76, 76, 57, 76, 57, 76, 76], [], []]

What I need to do is make another array a part of the global array (simple, something like global.offset = new Array() works fine). It needs to look something like this:

[[190, 266, 475, ...], [...], [], []]

Basically each place is the value thus far. As in global.offset[2] is the first three added, and so forth throughout the array.

But if I try something like:

for(i = 0, e < global.heights.length; i < e; i++) {
    for(j = 0, k < global.heights[1].length; j < k; j++) {
        global.offset[i][j] = Number(global.offset[i][j - 1]) + Number(global.heights[i][j]);
    }
}

If I do that I get an undefined error that global.offset is not an object (but it is already initialized and with four arrays inside it.

I am new to JS.

Thanks

A: 

This looks wrong:

    for(j = 0, k < global.heights[1].length; j < k; j++) {
        global.offset[i][j] = Number(global.offset[i][j - 1]) + Number(global.heights[i][j]);
    }

Do you mean k = global.heights[i].length (note the = and the replacement of 1 with i)?

Also you don't need to use Number() unless you're putting strings in the arrays.

That said without knowing where global is defined i'm unable to provide more help.

olliej
A: 

So the problem is that you are defining your array outside your loop. before you try to set the offset array, define it as an array.

This page does a better job of explaining what I mean: link text

IPX Ares
A: 

as IPX Ares says: declare your array outside the loop. Also ensure that "global.offset[i]" is instantiated before accessing the "[j]" member of "global.offset[i]".

global.offset = global.offset || []; // <- See this!
for(i = 0, e < global.heights.length; i < e; i++) {
    for(j = 0, k = global.heights[1].length; j < k; j++) { // I think you mean "k = global.heights[1].length"?
        global.offset[i] = global.offset[i] || []; // <- and this!
        global.offset[i][j] = Number(global.offset[i][j - 1]) + Number(global.heights[i][j]);
    }
}
Joshua