1

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

1 Answer 1

3

Your biggest mistake is here (inside Bank):

for (var j, j = 0; j <= 7; j++) {
    var clips = []
    var aClip = new Clip(w,j);
    //post(i)
    //post(aClip.length)
    clips[j] = aClip
}

You're re-initializing clips each 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 to Bank:

[undefined, undefined, undefined, undefined, undefined, 
     undefined, undefined, Clip { track=0, slot=7}]

Moving the declaration outside of the loop solves this basic problem:

var clips = [];
for (var j = 0; j <= 7; j++) {
    var aClip = new Clip(w, j);
    clips[j] = aClip;
}
return clips;

Additional Clean-up

Now that we've solved the basic problem, there's a lot more we could do to clean this up:

  • Use semi-colons consistently
  • Don't name functions with an initial uppercase letter unless you intend to use them as constructors
  • Indent consistently
  • Don't rely on global variables
  • Declare and assign in one step (i.e. var j, j = 0 should be var j = 0)

The result:

function Clip(a, b) {
    this.track = a;
    this.slot = b;
}

function makeBank(w) {
    var clips = [];
    for (var j = 0; j <= 7; j++) {
        var aClip = new Clip(w, j);
        clips[j] = aClip;
    }
    return clips;
}

function makeBanks() {
    var banks = [];
    for (var k = 0; k <= 7; k++) {
        var aBank = makeBank(k);
        banks[k] = aBank;
    }
    return banks;
}

var banks = makeBanks();
console.log(banks[0][0]);
Sign up to request clarification or add additional context in comments.

5 Comments

Many thanks for your suggestions, will endeavor to carry them out. In particular, you argue that I whould not use the global variable. How would I then make a banks array accessible to all functions?
@jamesson - Notice that bank is returned from makeBanks. The functional style encourages function parameters and return values over global variables. Simply pass that value along to any function that needs its.
reviewed the wiki on functional programming How in such a paradigm should we behave if we absolutely need a state? Example - I have a UI that behaves in 2 completely different manners based on whether a mousebutton is up or down.
@lwburk +1 for this pristine answer. Kudos to you for going above and beyond. Well done.
@jamesson - I'm not a purist. If you need state, then you need it. It didn't seem necessary in the contained example above, so I removed it. JavaScript supports multiple paradigms.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.