/    Sign up×
Community /Pin to ProfileBookmark

can’t get my two dimensional array to work

Below is a table that I am to produce but I cant seem to get the numbers in the right place. I think i wrote my array of the number in the wrong form, maybe it should look something like array[0][0] = 2; array[0][1] = 4, etc but i am not sure where to put it. Any suggestions?

0 2 4 6
8 10 12 14
16 18 20 22

function multidimensionalArray(table) {

document.write(“<table border=1>”);

for(var x=1; x<=3; x++) {
document.write(“<tr><td>”,x,”</td>”);
for(var y=0; y<=2; y++){
document.write(“<td>”,table[x][y],”</td>”);
}
document.write(“</tr>”);
}
document.write(“</table>”);
}
var array1 = new Array(“0″,”2″,”4″,”6”);
var array2 = new Array(“8″,”10″,”12”, “14”);
var array3 = new Array(“16″,”18″,”20″,”22”);
var newarray = new Array(“”,array1,array2,array3);

multidimensionalArray(newarray);

to post a comment
JavaScript

4 Comments(s)

Copy linkTweet thisAlerts:
@JMRKERFeb 22.2009 — <i>
</i>&lt;html&gt;
&lt;head&gt;
&lt;title&gt;MultiDim&lt;/title&gt;
&lt;script type="text/javascript"&gt;
// From: http://www.webdeveloper.com/forum/newreply.php?do=postreply&amp;t=202868

var NumArr = [
[0, 2, 4, 6,],
[8, 10, 12, 14],
[16, 18, 20, 22]
];

function multidimensionalArray(Arr) {
document.write("&lt;table border=1&gt;");
for(var x=0; x&lt;3; x++) {
document.write("&lt;tr&gt;");
for(var y=0; y&lt;4; y++){
document.write("&lt;td&gt;",Arr[x][y],"&lt;/td&gt;");
}
document.write("&lt;/tr&gt;");
}
document.write("&lt;/table&gt;");
}
&lt;/script&gt;
&lt;/head&gt;
&lt;body onload="multidimensionalArray(NumArr)&gt;

&lt;/body&gt;
&lt;/html&gt;
Copy linkTweet thisAlerts:
@ajpenn215authorFeb 22.2009 — thanks
Copy linkTweet thisAlerts:
@ajpenn215authorFeb 22.2009 — I was also wondering how would you sum the whole array? I think the sum is 156. How would I call

[0, 2, 4, 6,],

[8, 10, 12, 14],

[16, 18, 20, 22]

to sum the array?
Copy linkTweet thisAlerts:
@JMRKERFeb 23.2009 — <i>
</i>function multidimensionalArray(Arr) {
var sum = 0;
document.write("&lt;table border=1&gt;");
for(var x=0; x&lt;3; x++) {
document.write("&lt;tr&gt;");
for(var y=0; y&lt;4; y++){
document.write("&lt;td&gt;",Arr[x][y],"&lt;/td&gt;");
sum += Arr[x][y];
}
document.write("&lt;/tr&gt;");
}
document.write("&lt;/table&gt;&lt;br&gt;Sum: "+sum);
}
×

Success!

Help @ajpenn215 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 6.17,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @nearjob,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,

tipper: @meenaratha,
tipped: article
amount: 1000 SATS,
)...