英文:
Javascript - How to generate a "staggered" grid of numbers
问题
以下是代码的中文翻译:
我不太确定如何为这个命名,如果有人更清楚,欢迎指导。
基本上,使用嵌套的for循环,我试图生成一个二维数组,其中每一行都从前一行的数字高一个开始,然后在自身上进行循环,以使每一行的总和保持相等。让我更好地向您展示我的意思:
[
[1, 2, 3], // 1 + 2 + 3 = 6
[2, 3, 1], // 2 + 3 + 1 = 6
[3, 1, 2], // 3 + 1 + 2 = 6
]
看到了吗?每一行都是1、2、3,但每一行后面都是从i + 1开始,当它达到3时会“重置”回1。这就是我试图实现的效果。
这是我目前的代码:
const grid = []
const gridSize = 3
for(let i = 0; i < gridSize; i++) {
const row = []
for(let j = 0; j < gridSize; j++) {
row.push(/* 这里应该放什么?这是我卡住的地方 */)
}
grid.push(row)
}
希望这个翻译对你有帮助。如果你有任何其他问题,可以继续提问。
英文:
I'm not really sure how to title this one, so if someone knows better, be my guest.
Basically, using nested for-loops, I'm trying to generate a 2-d array where each row starts at one number higher than the previous row, and then loops around on itself, so that the total of each row remains equal. Let me rather just show you what I mean:
[
[1, 2, 3], // 1 + 2 + 3 = 6
[2, 3, 1], // 2 + 3 + 1 = 6
[3, 1, 2], // 3 + 1 + 2 = 6
]
See how every row is just 1, 2, 3 but every following row starts at i + 1 and when it reaches 3 it "resets" back to 1? That's what I'm trying to achieve.
Here's what I have so far:
const grid = []
const gridSize = 3
for(let i = 0; i < gridSize; i++) {
const row = []
for(let j = 0; j < gridSize; j++) {
row.push(/* what goes here?? this is where I get stuck */)
}
grid.push(row)
}
答案1
得分: 2
以下是代码部分的翻译:
// 使用索引相加并取大小的模,如果希望值从1(偏移)开始,只需在映射每个单元格的结果上加1。
const generateMatrix = (size, offset = 1) =>
Array.from({ length: size }, (_, rowIndex) =>
Array.from({ length: size }, (__, colIndex) =>
((rowIndex + colIndex) % size) + offset));
const formatMatrix = (matrix) => matrix.map(row => row.join(' ')).join('\n');
console.log(formatMatrix(generateMatrix(2)));
console.log(formatMatrix(generateMatrix(3)));
console.log(formatMatrix(generateMatrix(4)));
// 这是使用传统的for循环的版本。
const generateMatrix = (size, offset = 1) => {
const matrix = [];
for (let rowIndex = 0; rowIndex < size; rowIndex++) {
const row = [];
for (let colIndex = 0; colIndex < size; colIndex++) {
row.push(((rowIndex + colIndex) % size) + offset);
}
matrix.push(row);
}
return matrix;
};
const formatMatrix = (matrix) => matrix.map(row => row.join(' ')).join('\n');
console.log(formatMatrix(generateMatrix(2)));
console.log(formatMatrix(generateMatrix(3)));
console.log(formatMatrix(generateMatrix(4)));
英文:
You can add the row and column index together and mod by the size. If you want the values to start at 1 (offset), just add 1 to the result of each cell when mapping.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const generateMatrix = (size, offset = 1) =>
Array.from({ length: size }, (_, rowIndex) =>
Array.from({ length: size }, (__, colIndex) =>
((rowIndex + colIndex) % size) + offset));
const formatMatrix = (matrix) => matrix.map(row => row.join(' ')).join('\n');
console.log(formatMatrix(generateMatrix(2)));
console.log(formatMatrix(generateMatrix(3)));
console.log(formatMatrix(generateMatrix(4)));
<!-- language: lang-css -->
.as-console-wrapper { top: 0; max-height: 100% !important; }
<!-- end snippet -->
Here is a version with traditional for-loops:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const generateMatrix = (size, offset = 1) => {
const matrix = [];
for (let rowIndex = 0; rowIndex < size; rowIndex++) {
const row = [];
for (let colIndex = 0; colIndex < size; colIndex++) {
row.push(((rowIndex + colIndex) % size) + offset);
}
matrix.push(row);
}
return matrix;
};
const formatMatrix = (matrix) => matrix.map(row => row.join(' ')).join('\n');
console.log(formatMatrix(generateMatrix(2)));
console.log(formatMatrix(generateMatrix(3)));
console.log(formatMatrix(generateMatrix(4)));
<!-- language: lang-css -->
.as-console-wrapper { top: 0; max-height: 100% !important; }
<!-- end snippet -->
答案2
得分: 1
你可以使用 Array(n).fill()
来填充数组。使用内部函数生成的值来进行 map
操作。它返回从当前数组索引开始的子数组。取模 % 用于在达到数组末尾时重新开始。+1 用于将值偏移,使它们从1开始而不是0。
const n = 3;
const arr2D = Array(n)
.fill()
.map((_, i) => Array(n)
.fill()
.map((_, j) => ((i + j) % n) + 1)
);
console.log(arr2D);
请注意,上面的代码用于创建一个二维数组,并将其打印出来。
英文:
You can use Array(n).fill()
to populate arrays. map
the values generated with the inner function. It returns the sub-arrays that start at the current array index. Modulo % is used to wrap the sub-array when it reaches the end. +1 is used to shift the values so that they start at 1 instead of 0.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const n = 3;
const arr2D= Array(n)
.fill()
.map((_, i) => Array(n)
.fill()
.map((_, j) => ((i + j) % n) + 1)
);
console.log(arr2D)
<!-- end snippet -->
答案3
得分: 1
以下是代码的翻译部分:
这是我发现易于阅读的一种方法。
// 开始代码段:JavaScript 部分可见,控制台输出为真,Babel 编译为假
// JavaScript 语言
const gridSize = 3;
let grid = [];
let row = [];
// 构建原始行
for (let i = 1; i <= gridSize; i++) {
row.push(i);
}
// 将原始行添加到网格
grid.push(row);
// 通过值复制创建行的副本 https://stackoverflow.com/a/7486130/1024832
let newRow = row.slice();
for (let i = 1; i < gridSize; i++) {
// 复制先前的行(通过值)
newRow = newRow.slice();
// 移除第一个元素
let firstElement = newRow.shift();
// 将移除的元素附加到行的末尾
newRow.push(firstElement);
// 将新行添加到网格
grid.push(newRow);
}
console.log(grid);
// 结束代码段
希望这对您有帮助。如果您有任何其他问题或需要进一步的帮助,请随时提出。
英文:
Here's an approach that I find easy to read.
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-js -->
const gridSize = 3;
let grid = [];
let row = [];
//build the original row
for (let i = 1; i <= gridSize; i++) {
row.push(i);
}
//push the original row onto the grid
grid.push(row);
//make a copy of the row by value https://stackoverflow.com/a/7486130/1024832
let newRow = row.slice();
for (let i = 1; i < gridSize; i++) {
//copy the prior row (by value)
newRow = newRow.slice();
//shift the first element
let firstElement = newRow.shift();
//append the shifted element to the end of the row
newRow.push(firstElement);
//push the new row onto the grid
grid.push(newRow);
}
console.log(grid);
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论