1

I have an array such as this:

var array = [['h','e','l','l','o'],['1','2','3','4','5'],['a','b','c','d','e']]

and I am having trouble passing it to a function here is the original script I am using:

for (var x = 0; x <= 2; x++) {
    var timesrun = 0;

    function runcode() {
        timesrun += 1;
        for (var n = 0; n <= 4; n++) {
            console.log(array[x][n]);
        } //end for loop 1
        if (timesrun == 2) {
            clearInterval(interval);
        }
    } //end of function
} // end for loop 2
var interval = setInterval(function () {
    runcode(array[x]);
}, 1000);

When I console.log inside the function I get nothing but if I take the inner for loop outside the function and then console.log I get the expected values So I don't think I'm bringing the values into the function correctly.

For simplicity's sake I would like to ask this question using the simple example below:

function runcode(?){
    console.log(array[0][1]);  //which should return h.
}
runcode(?);
1

2 Answers 2

1
var array = [['h','e','l','l','o'],['1','2','3','4','5'],['a','b','c','d','e']],
    x = 0,
    timesrun = 0;

function runcode() {
    timesrun += 1;
    for (var n = 0; n <= 4; n++) {
        console.log(array[x][n]);
    }
    if (timesrun == 2) {
        clearInterval(interval);
    }
}    

var interval = setInterval(function () {
    for (x = 0; x <= 2; x++) {
        runcode(array[x]);
    }
}, 1000);
Sign up to request clarification or add additional context in comments.

4 Comments

He wants to use setInterval!
Thank you! you saved many hours of anguish lol. I think I can definitely continue from here :)
although it does seem I must use array[x-1][n] inside the function to get all of them or else it crashes cause it skips the first one. would you maybe know why this is?
What is setInterval doing there, its essentially the same as doing a setTimeout as on the fist invocation timesrun === 3 hence the interval is getting cleared. Also the array passed to runcode is never used in it. And i think thats waht the OP's question was about
0

For simplicity's sake I would like to ask this question using the simple example below:

function runcode(?){
    console.log(array[0][1]);  //which should return h.
}

runcode(?);

To access the a passed variable, you have to name the parameters in the functions header

so if you would replace ? with array you are already good to go

function runcode(array){
              // ^^^^^ This is the name under which you can acces the passed variable
    console.log(array[0][1]);  //which should return h.
              //^^^^^ This is where you use it

}
runcode(array);
      //^^^^^ this is where you pass it //could be someOther2DimensionalArray

And applied to the nonworking code

var array = [
    ['h', 'e', 'l', 'l', 'o'],
    ['1', '2', '3', '4', '5'],
    ['a', 'b', 'c', 'd', 'e']
],
    x = 0,
    timesrun = ~0;

function runcode(array) {
    x = timesrun += 1;
    for (var n = 0; n <= 4; n++) {
        console.log(array[x][n]);
    }
    if (timesrun == 2) {
        clearInterval(interval);
    }
}

var interval = setInterval(runcode, 1000, array);

this should do the trick.

Note the third parameter of setInterval is used to pass array //runcode(array)

Comments

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.