jamham0
jamham0

Reputation: 19

how do I make a 2d array function

Im trying to make a function that makes a 2d array but I have a syntax error here is the function

function asdf(col,row)
{
    let arr = new Array(col);
    let i = 0;
    for (i < col; i++)
    {
        arr[i] = new Array(row);
    }
    return arr;
}

the error is at the ")" at the end of the for loop any ideas?

Upvotes: 0

Views: 43

Answers (2)

Khalil
Khalil

Reputation: 1515

The error is due to a missing semi-colon in the for loop:

for (i < col; i++)

Change it to

for (;i < col; i++)

A better approach can be just to simply define the loop variable in the for loop like so:

for (let i = 0;i < col; i++)

Upvotes: 2

Hazik Arshad
Hazik Arshad

Reputation: 514

The for statement creates a loop with 3 optional expressions:

for (expression 1; expression 2; expression 3) {
  // code block to be executed
}

Expression 1 is executed (one time) before the execution of the code block.

Expression 2 defines the condition for executing the code block.

Expression 3 is executed (every time) after the code block has been executed.

There's a syntax error because you are missing Expression 1.

Upvotes: 0

Related Questions