0

I've got 2 functions. One to create 2D array, the other to print it. Something is wrong with the creating function, because when I create and send an 2D array by myself, everything is fine. But when I try to do this through the create funcion, the console is empty (but no errors). Could you help me?

function print2DArray (array2D) {
    for (var i = 0; i < array2D.length; i++) {
        for (var j = 0; j < array2D[i].length; j++) {
            console.log(array2D[i][j]);
        }
    }
}

function create2DArray (rows, columns) {
    var array2D = [];
    for (var i = 0, x = 0; i < rows; i++, x++) {
        array2D[i] = x;
        for (var j = 0; j < columns; j++) {
            x++;
            array2D.push(x);
        }
    }
    return array2D;
}

/*                 ← Here everything is fine
var arr = [
    [11, 12],
    [42, 2],
    [-4, -120],
    [0, 0],
    [1, 34],
];

print2DArray(arr);
 */

print2DArray(create2DArray(2, 3));       ← Empty console

2 Answers 2

1

The problem is that you don't create a 2D array as far more a 1D array.

In each iteration step you have to create an array and push it to your array to create a 2D one.

function print2DArray (array2D) {
    for (var i = 0; i < array2D.length; i++) {
        for (var j = 0; j < array2D[i].length; j++) {
            console.log(array2D[i][j]);
        }
    }
}

function create2DArray(rows, columns) {
    var array2D = [];
    
    for (var i = 0; i < rows; i++) {
        let temp = [];
        for (var j = 0; j < columns; j++) {
                temp[j] = Math.floor(Math.random() * Math.floor(100))
        }
        array2D.push(temp);
    }
    return array2D;
}

/*                 ← Here everything is fine
var arr = [
    [11, 12],
    [42, 2],
    [-4, -120],
    [0, 0],
    [1, 34],
];

print2DArray(arr);
 */

print2DArray(create2DArray(2, 3));       //← Empty console

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

Comments

0

You have created an single dimension array which is why its going into the 2nd loop.

function print2DArray (array2D) {
   // console.log(array2D)
    for (var i = 0; i < array2D.length; i++) {
        for (var j = 0; j < array2D[i].length; j++) {
            console.log(array2D[i][j]);
        }
    }
}

function create2DArray (rows, columns) {
    var array2D = [];
    for (var i = 0, x = 0; i < rows; i++, x++) {
        array2D[i] = []
        for (var j = 0; j < columns; j++) {
            
            array2D[i].push(x);
            x++;
        }
    }
    return array2D;
}

/*                 ← Here everything is fine
var arr = [
    [11, 12],
    [42, 2],
    [-4, -120],
    [0, 0],
    [1, 34],
];

print2DArray(arr);
 */

print2DArray(create2DArray(2, 3));  

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.