'2d array with function javascript

I've task to create function that will display names from 2d array (const users). Each name should be displayed in separate line in console. Here is the code I've prepared but not sure if it's correct?

const users = [["Jaydn Humphries", "Ayda Orozco"], ["Sanjeev Wilkinson", "Jorge Markham"]];

function print2DArray(array) {
    for(let i = 0; i < array.length; i++){
        for(let j = 0; j < array.length; j++) {
            console.log(array[i][j]);
        }
    }return array;
}
print2DArray(users);


Solution 1:[1]

Your inner for loop should run on the inner array not on the outer array. i.e. j should be looped up to array[i].length not array.length

Although since in your example the inner arrays have the same length as the outer array, your example should work just fine.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 BH385