1

I have an array of arrays (a multi-dimensional array so to speak, doors in this example), in which wait and point are undefined.

var wait;
var point;
var doors = [
    [wait, doorWrap, 'regY', point, 10, 'bounceOut', 150, 5, 'bounceIn']
// ,[etc.]
];

I want to loop the doors array and for every iteration, execute the key() function, with the doors entries as arguments.

multiKey(doors, 500, 600);

function multiKey (keys, point, wait) {
    for (var i = 0; i < keys.length; i++) {
            wait *= i;
            key.apply(this, keys[i]);
    }
}

Having passed 500 and 600 into the multiKey() function, I expected that point and wait would be defined before the key() function would run -- but heck, point and wait turn out to be undefined.

What is wrong? How could I go about solving this?

Sorry for the title too. I hope the question is clear enough though, because I had a hard time putting my problem into words! Thanks.

6
  • 1
    You don't define a key function anywhere. So that's probably why it's undefined... And that's the only real problem that you say you are having. It's not clear what you are after here. Commented Jun 27, 2013 at 23:59
  • @AlexWayne Sorry, I omitted the key() function here because I didn't think it was relevant. But it's in my code. Commented Jun 28, 2013 at 0:02
  • The wait in your multiKey() signature and the wait in your doors Arrays are different values. Commented Jun 28, 2013 at 0:03
  • Your only stated problem is that key is undefined, and you didn't think that included how to you define key is relevant? What is the code you have, and what exactly is wrong with it? Commented Jun 28, 2013 at 0:05
  • @AlexWayne: I think he's saying that wait and point are undefined inside the key function. Seems that there's an assumtion that all the wait identifiers will automatically resolve to the same memory. Commented Jun 28, 2013 at 0:08

2 Answers 2

1

The wait in your multiKey() signature and the wait in your doors Arrays are different values.

You could assign the values you're passing to each Array in the loop.

var doors = [
// --v--wait            --v--point
    [0, doorWrap, 'regY', 0, 10, 'bounceOut', 150, 5, 'bounceIn']
// ,[etc.]
];

multiKey(doors, 500, 600);

function multiKey (keys, point, wait) {
    for (var i = 0; i < keys.length; i++) {
            keys[i][0] = wait * i;
            keys[i][3] = point
            key.apply(this, keys[i]);
    }
}

JavaScript doesn't have pointers, and its primitive types are always copied when assigned. As such, you're copying undefiend into the doors Arrays when you include wait and point in them.

Then when you pass the initial values to the multiKey function, they're being assigned to the wait and point parameters, which are entirely different. You're then multiplying the wait parameter by i, but again, that's a completely different value from that in the Arrays, so you're still just passing undefined from the Array.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks @CrazyTrain! I actually tried this before. Somehow didn't get it to work then. Now it works perfectly!
0

point and wait are declared but not defined.

I see no operation on point.

1 Comment

I passed 500 and 600 as point and wait respectively as parameters to the multiKey function. A console.log does return 500 and 600, but it just isn't passed to the key function.

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.