谁能解释这段javascript代码
vara=newArray(5);for(vari=0;i<a.length;i++){a[i]=newArray(5);}varsum=0;varstr="";for(...
var a = new Array(5);
for(var i = 0 ; i < a.length; i++){
a[i] = new Array(5);
}
var sum = 0;
var str = "";
for( i = 0 ; i<a.length;i++){
for(var j = 0 ; j<a[i].length;j++){
a[i][j] = i+j;
str += a[i][j]+" "
if(i == j){
sum += a[i][j];
}
}
str +="<br/>";
}
alert(sum);
alert(str);
document.write("<div id='show'>");
document.write(str);
document.write("</div>"); 展开
for(var i = 0 ; i < a.length; i++){
a[i] = new Array(5);
}
var sum = 0;
var str = "";
for( i = 0 ; i<a.length;i++){
for(var j = 0 ; j<a[i].length;j++){
a[i][j] = i+j;
str += a[i][j]+" "
if(i == j){
sum += a[i][j];
}
}
str +="<br/>";
}
alert(sum);
alert(str);
document.write("<div id='show'>");
document.write(str);
document.write("</div>"); 展开
1个回答
展开全部
var a = new Array(5);
for(var i = 0 ; i < a.length; i++){
a[i] = new Array(5);
} //到这里以上代码建立5*5二维数组
var sum = 0;
var str = "";
for( i = 0 ; i<a.length;i++){
for(var j = 0 ; j<a[i].length;j++){
a[i][j] = i+j;
str += a[i][j]+" "
if(i == j){
sum += a[i][j];
}
}
'以上两重循环,给二维数组 赋值
str +="<br/>";
}
alert(sum);
alert(str);
document.write("<div id='show'>"); //以下定义一个div标签,再输出具体内容
document.write(str);
document.write("</div>");
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询