The goal of the following code is to create a 2d array such that
- Clip is a custom object
- a Bank is an array of (8) clips
- Banks is an array of (8) banks
- Each clip is accessible by banks[a][b], where a is an index in banks (a bank) and b is an index in clips (a clip)
In its current state it returns undefined, alas I am at a loss to explain why. Any suggestions about what I’m doing wrong would be greatly appreciated
var banks = []
function Clip(a,b)
{
this.track = a
this.slot = b
}
function Bank(w)
{
for (var j, j = 0; j <= 7; j++) {
var clips = []
var aClip = new Clip(w,j);
//post(i)
//post(aClip.length)
clips[j] = aClip
}
//post();
return clips
}
function makeBanks()
{
for (var k, k = 0; k <= 7; k++) {
var aBank = Bank(k);
//post(i)
//post (aClip.length)
banks[k] = aBank
}
}
makeBanks();
console.log(banks[0][0])
Many thanks in advance
Your biggest mistake is here (inside
Bank):You’re re-initializing
clipseach time through the loop, so that the only value that is retained is the last one (since the loop terminates before you get a chance to overwrite it again). To illustrate, this is what’s returned from the first call toBank:Moving the declaration outside of the loop solves this basic problem:
Additional Clean-up
Now that we’ve solved the basic problem, there’s a lot more we could do to clean this up:
var j, j = 0should bevar j = 0)The result: