Welcome to OStack Knowledge Sharing 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 Answer

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);

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...