Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create a 2d array which dimensions are specified by user (in jQuery)

I need to create a 2D array with dimension specified in input fields. And fill the generated fields so that it spirals clock-wise, starting from the bottom right corner.

enter image description here

I managed to create the 2D array but I don't know how to fill it. Any help is appreciated.

$("button").click(function()
{
    var brr=parseInt($("#brr").val());
    var brs=parseInt($("#brs").val());
    for (i=1; i<=brs; i++)
    {
        for (j=1; j<=brr; j++)
        {
            $("#output").append("<div class='k'>o</div>");
        }
        $("#output").append("<br/>");
    }

})
like image 625
Veka Avatar asked Nov 09 '22 21:11

Veka


1 Answers

something like this?

http://jsfiddle.net/mig1098/d0beygLp

$("#button").click(function()              
{        
    $("#output").html('');
    var brs1=parseInt($("#brs1").val());
    var brs2=parseInt($("#brs2").val());
    var c=0;
    for (i=1; i<=brs1; i++){

     for (j=1; j<=brs2; j++){
        $("#output").append("<div class='k'>"+c+"</div>");
         c++;
     }
    $("#output").append("<div class=\"clearfix\"></div>");
    }

});
like image 75
miglio Avatar answered Nov 14 '22 23:11

miglio