2

hey guys i have a situation here. i have a code that out puts a two dimensional here it is.

<script>
    Brady = new Array(3);

    for (i = 0; i < Brady.length; ++i) {
        Brady[i] = new Array(5);
    }

    Brady[0][0] = "Team 1";
    Brady[0][1] = "Carol";
    Brady[0][2] = "Greg";
    Brady[0][3] = "Greg";
    Brady[0][4] = "Greg";

    Brady[1][0] = "Team 2";
    Brady[1][1] = "Alice";
    Brady[1][2] = "Peter";
    Brady[1][3] = "Peter";
    Brady[1][4] = "Peter";

    Brady[2][0] = "Team 3";
    Brady[2][1] = "Mike";
    Brady[2][2] = "Bobby";
    Brady[2][3] = "Bobby";
    Brady[2][4] = "Bobby";




    function print_2d_string_array(array) {
        document.writeln("<table border>");

        var row;

        for (row = 0; row < array.length; ++row) {
            document.writeln(" <tr>");

            var col;

            for (col = 0; col < array[row].length; ++col) {
                document.writeln("  <td>" + array[row][col] + "</td>");
            }

            document.writeln(" </tr>");
        }
        document.writeln("</table>");
    }

    print_2d_string_array(Brady);
</script>

with an out put of:

Team 1  Carol   Greg    Greg    Greg
Team 2  Alice   Peter   Peter   Peter
Team 3  Mike    Bobby   Bobby   Bobby

but what i want to do is

Team 1  Team 2  Team 3
Carol   Alice   Mike
Greg    Peter   Bobby
Greg    Peter   Bobby
Greg    Peter   Bobby

ive been spending an hour to do that but im out of luck.. plss help me.

2 Answers 2

4

Try:

<script>
Brady = new Array(3);

for (i = 0; i < Brady.length; ++i) {
    Brady[i] = new Array(5);
}

Brady[0][0] = "Team 1";
Brady[0][1] = "Carol";
Brady[0][2] = "Greg";
Brady[0][3] = "Greg";
Brady[0][4] = "Greg";

Brady[1][0] = "Team 2";
Brady[1][1] = "Alice";
Brady[1][2] = "Peter";
Brady[1][3] = "Peter";
Brady[1][4] = "Peter";

Brady[2][0] = "Team 3";
Brady[2][1] = "Mike";
Brady[2][2] = "Bobby";
Brady[2][3] = "Bobby";
Brady[2][4] = "Bobby";


function print_2d_string_array(array) {
    document.writeln("<table border>");

    var col_len = array.length;
    var row_len = array[0].length;

    for (var i = 0; i < row_len; i++) {
        document.writeln("<tr>");
        for (var j = 0; j < col_len; j++) {
           document.writeln("<td>" + array[j][i] + "</td>");
        }
        document.writeln("<tr>");
    }
    document.writeln("</table>");
}

print_2d_string_array(Brady);​
</script>

This will output:

Team 1  Team 2  Team 3
Carol   Alice   Mike
Greg    Peter   Bobby
Greg    Peter   Bobby
Greg    Peter   Bobby

Here's a JSFiddle example: http://jsfiddle.net/uNPXa/

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

6 Comments

what does the array[0].length; do?
It gets the inner length of one of your two-dimensional arrays, retrieving the amount of rows.
dude can would you mind explaining to me how the loop happen, cuz im kinda confused with the array[0].length; and how it is incrementing. cuz i supposed that the array[0].length; will = Brady[0] how does it get the Brady[1] and Brady[2]
This solution assumes Brady[0].length is equivalent to Brady[1].length and Brady[2].length
how does the 0 increment if the array[0].length; have the index of 0?
|
1

You should change your for loop like this:

for (row = 0; row < array[0] . length; ++ row)
{
    document . writeln (" <tr>");
    var col;
    for (col = 0; col < array . length; ++ col)
    {
            document . writeln ("  <td>" + array [col][row] + "</td>");
    }
    document . writeln (" </tr>");
}

Notice the changes that I have made in row < array[0] . length and array . length

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.