编写一个函数 行 列 n=5 m=7;
[
[1, 10, 11, 20, 21, 30, 31],
[2, 9, 12, 19, 22, 29, 32],
[3, 8, 13, 18, 23, 28, 33],
[4, 7, 14, 17, 24, 27, 34],
[5, 6, 15, 16, 25, 26, 35]
]
function arrList(n, m) {
// 创建外围数组
let arr = [];
for (let index = 1; index <= n; index++) {
let createArr = []
createArr[0] = index
arr.push(createArr)
}
// 算法基数
let a = n * 2 - 1, b = 1;
// 奇数列计算函数
function addOdd(value) {
return value + a
}
// 偶数列计算函数
function addEven(value) {
return value + b
}
// 循环行
for (let i = 0; i < n; i++) {
// 循环列
for (let index = 1; index < m; index++) {
// 奇数列
if (index % 2 == 1) {
let arr1 = arr[i]
let value = addOdd(arr1[index - 1])
arr1.push(value)
}
// 偶数列
if (index % 2 == 0) {
let arr1 = arr[i]
let value = addEven(arr1[index - 1])
arr1.push(value)
}
// 最后一列更新算法
if (index == m - 1) {
// 更新算法
a = (n * 2 - 1) - (i + 1) * 2;
b = 1 + (i + 1) * 2;
}
}
}
return arr
}
console.log(arrList(6, 8))