Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have been trying to get array values to form a table and I am almost there, I just keep running into one issue. My table has 7 columns and 6 rows. I was required to have an array for each of the variables but when I actually create the table 3 of the arrays have to be put together and then each value for each of the three arrays put in the column down. This is what I have accomplished so far for code:
<br />
// constants<br />
var colCount = 7;<br />
var rowCount = 7;<br />
<br />
// input data<br />
var id = new Array("1022", "1112", "1230", "554", "1355", "1600");<br />
var title = new Array("Prof.", "Prof.", "Prof.", "Prof.", "Asst. Prof.", "Asst. Prof.");<br />
var name = new Array("Catherine Adler", "Michael Li", "Maria Sanchez", "Robert Hope", "Wayne Li", "Kate Howard");<br />
var position = new Array("Department Chair", "Asst. Chair", "TA Supervisor");<br />
var er = new Array(2, 3, 1, 2, 1, 3);<br />
var yoe = new Array(18, 12, 10, 23, 8, 5);<br />
var cs = new Array(85000, 70000, 62000, 55000, 50000, 45000);<br />
<br />
// make a special column.<br />
var complicatedName;<br />
for (index = 0; index < rowCount; index++)<br />
{<br />
   complicatedName[index] = title[index]+' '+name[index]+' '+position[index];<br />
}<br />
colCount = colCount - 2<br />
<br />
// create the column array.<br />
var collist = [id,complicatedName,er,yoe,cs];<br />
<br />
// make the table.<br />
function tableCreate() {<br />
 for (rownum = 1; rownum <= rowCount; rownum++) {<br />
     document.write("<table><tbody><tr>");<br />
     for (colnum = 1; colnum <= colCount; colnum++) {  <br />
         document.write("<td>" + (collist[(colnum-1)])[(rownum-1)] + "</td>");<br />
     }<br />
     document.write("</tr></tbody></table>");<br />
 }<br />
}<br />

I find that the complicatedName and collist come out to undefined for some strange reason and have no ideas as to why. Index also appears to be undefined.
Any insights would be helpful and if extra clarification is needed please ask.
Posted
Updated 22-May-11 20:03pm
v4
Comments
Graham Breach 23-May-11 2:04am    
Replaced > and < with &gt; and &lt;

y your position array got only 3 elements in it...
won't it fail in 3rd iteration looking for position[3] and throw null exception...

Also set
<br />
 var complicatedName as Array;<br />
 
Share this answer
 
v3
Comments
Hemant__Sharma 23-May-11 2:05am    
Very good catch... my 5.

--Hemant
saxenaabhi6 23-May-11 2:28am    
thanks :)
Graham Breach 23-May-11 5:09am    
No, in Javascript accessing a non-existent member of an array will give you undefined, but not throw an exception.

var complicatedName as Array; is not valid Javascript either.
saxenaabhi6 23-May-11 6:58am    
thanks for correcting me
but i dont know y, this 'as array' worked for me it fixes above code.
I don't see you access the array elements in complicatedName, only accessing the array as a whole ?
 
Share this answer
 
Comments
Mikey Gmc 22-May-11 17:44pm    
The elements have to be together as a whole. Basically it is the title of the employer, their name, and the job position they have. row 1, column to would theoretically have: Prof. Catherine Alder Department Chair in the table cell.
Change that line where you are defining complicatedName

var complicatedName = new Array();
 
Share this answer
 
The main problem is that you try to add something to complicatedName[index] without setting it up as an array first:
var complicatedName = [];
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900