Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.8k views
in Technique[技术] by (71.8m points)

js 在控制台输出4行4列0,其中2个数字被2,4替代,且刷新一次,2,4数字会相应改变?

突然想到一个小练习,想在控制台输出类似
0 0 0 0
0 2 0 0
0 0 4 0
0 0 0 0
数组类型对的练习,图中2,4位置每刷新一次都会在4行4列中随意位置出现,分析思路如下:
1写初始化4行4列00000界面;
2写随机数函数;
3写刷新函数;
第一个初始化界面程序应该怎么写?想到了用for函数嵌套,好久没练习,写不出来,请教!
初始化界面如下:
0 0 0 0
0 0 0 0
0 0 0 0
0 0 0 0


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
let arr = Array(4*4).fill(0);
let temp = '
';
for (let index in arr) {
  temp += arr[index]+' ';
  if (index % 4 === 3) {
    temp += '
';
  }
}
console.log(temp);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...