Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 02-02-2013, 07:05 AM   PM User | #1
techyworld
New to the CF scene

 
Join Date: Feb 2013
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
techyworld is an unknown quantity at this point
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>
techyworld is offline   Reply With Quote
Old 02-02-2013, 10:59 AM   PM User | #2
Airblader
Regular Coder

 
Join Date: Jan 2013
Location: Germany
Posts: 374
Thanks: 3
Thanked 44 Times in 44 Posts
Airblader can only hope to improve
Like this?

Code:
function getRandom(e,t){return Math.floor(Math.random()*(t-e+1))+e}function drawTable(){var e=document.getElementById("div1");var t=document.createElement("table");t.setAttribute("id","table_id");t.setAttribute("border","1");e.appendChild(t);var n=document.getElementById("table_id");var r=document.getElementById("div2");var i=" ";for(var s=0;s<10;s++){var o=document.createElement("tr");totalRows[s]=new Array(10);for(var u=0;u<10;u++){var a=document.createElement("td");a.setAttribute("width","100px");a.setAttribute("height","35px");var f=getRandom(0,1e3);if(f%2==0){totalRows[s][u]=document.createTextNode(getRandom(min,max))}else{var l=getRandom(0,1e3);if(l%2==0){totalRows[s][u]=document.createTextNode("+")}else{totalRows[s][u]=document.createTextNode("-")}}if(u%3===2){a.style.color="red"}a.appendChild(totalRows[s][u]);o.appendChild(a)}n.appendChild(o)}}var totalRows=new Array(10);var min=1;var max=10;drawTable()
I decided to post code that is as unreadable as yours. I strongly suggest you get an editor that supports intendation and that you actually use that to make your code somewhat readable.
Airblader is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 09:57 PM.


Advertisement
Log in to turn off these ads.