www.webdeveloper.com
Results 1 to 2 of 2

Thread: Retrieving from a 2D array

  1. #1
    Join Date
    Feb 2013
    Posts
    1

    Retrieving from a 2D array

    Hi i have created a table which display operators and operands randomnly. I want to retrieve every 3 element in each row. How can i proceed? can someone help?

    Code:
    <script type="text/javascript">
    var totalRows = new Array(10);
    var min =1;
    var max =10;
    function getRandom(x,y)
    {return Math.floor(Math.random() * (y - x + 1)) + x;}
    function drawTable() {
    var div_id = document.getElementById('div1');
    var tbl = document.createElement("table");
    tbl.setAttribute("id","table_id") ;
    tbl.setAttribute("border","1") ;
    div_id.appendChild(tbl);
    var tabl_id = document.getElementById("table_id");
    var div_id2 = document.getElementById('div2');
    var node= " ";
    for(var i=0;i<10;i++){
    var row=document.createElement('tr');
    totalRows[i]= new Array(10);
    for(var j=0;j<10;j++){
    var cell=document.createElement('td');
    cell.setAttribute("width","100px") ;
    cell.setAttribute("height","35px") ;
    var k = getRandom(0,1000);
    if ((k%2)==0)
    {
    totalRows[i][j] = document.createTextNode(getRandom(min,max));
    }
    else{
    var z=getRandom(0,1000);
    if ((z%2)==0)
    {totalRows[i][j] = document.createTextNode("+");}
    else
    {totalRows[i][j] = document.createTextNode("-");}
    }
    cell.appendChild(totalRows[i][j]);
    row.appendChild(cell);
    }
    tabl_id.appendChild(row);
    }
    } 
    </script>

  2. #2
    Join Date
    Jun 2004
    Location
    Portsmouth UK
    Posts
    2,682
    Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    
    <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
    
    <head>
      <title></title>
    </head>
    
    <body>
    <div id="div1" >TOM</div>
    <script> vic=0; </script>
    <form name=Show id=Show style="position:absolute;visibility:visible;top:700px;left:0px;" >
    <input size=100 name=Show0 >
    <input size=10 name=Show1 >
    <input size=10 name=Show2 >
    <input size=10 name=Show3 >
    <input size=10 name=Show4 >
    <input size=10 name=Show5 >
    <input size=10 name=Show6 >
    <input size=10 name=Show7 >
    <input size=10 name=Show8 >
    <input size=10 name=Show9 ><br>
    <textarea name=TA rows=1 cols=100 ></textarea>
    </form>
    
    <script type="text/javascript">
     var totalRows = new Array(10);
     var min =1;
     var max =10;
    
    function getRandom(x,y){
     return Math.floor(Math.random() * (y - x + 1)) + x;
    }
    
    function drawTable() {
     var div_id = document.getElementById('div1');
     var tbl = document.createElement("table");
     var tbdy = document.createElement("TBODY");
      tbl.appendChild(tbdy);
     tbl.setAttribute("id","table_id") ;
     tbl.setAttribute("border","1") ;
     div_id.appendChild(tbl);
     var tabl_id = document.getElementById("table_id");
     var div_id2 = document.getElementById('div2');
     var node= " ";
     for(var i=0;i<10;i++){
      var row=document.createElement('tr');
      totalRows[i]= new Array(10);
      for(var j=0;j<10;j++){
       var cell=document.createElement('td');
       cell.setAttribute("width","100px") ;
       cell.setAttribute("height","35px") ;
       var k = getRandom(0,1000);
       if ((k%2)==0){
        totalRows[i][j] = document.createTextNode(getRandom(min,max));
       }
       else {
        var z=getRandom(0,1000);
        if ((z%2)==0){
         totalRows[i][j] = document.createTextNode("+");
        }
        else {
         totalRows[i][j] = document.createTextNode("-");
        }
       }
       cell.appendChild(totalRows[i][j]);
       row.appendChild(cell);
       if (j==2){
    document.Show.Show0.value+=totalRows[i][j].data+'#';
    
       }
      }
      tbdy.appendChild(row);
     }
    }
    
     drawTable()
    /*]]>*/
    </script>
    
    </body>
    
    </html>
    Vic

    God loves you and will never love you less.

    http://www.vicsjavascripts.org/Home.htm
    If my post has been useful please donate to http://www.operationsmile.org.uk/

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles