function vortex(n, m) {
const nums = new Array(n).fill(0).map(() => new Array(m).fill(0))
let i = 0
let j = 0
let count = 1
let stepI = 0
let stepJ = 1
function _hasBlock() {
return !nums[i + stepI] || nums[i + stepI][j + stepJ] !== 0
}
while (1) {
nums[i][j] = count++
if (_hasBlock()) {
if (stepI === 0) {
stepI = stepJ
stepJ = 0
} else {
stepJ = -stepI
stepI = 0
}
if (_hasBlock()) {
break
}
}
i += stepI
j += stepJ
}
return nums
}
console.log(vortex(5, 6));
Javascript实现漩涡型二维数组案例
最新推荐文章于 2024-06-28 10:13:21 发布