前言
实现如下图的旋涡数组,简单理解为遇到拐点自动拐弯,否则一直沿当前方向推进。
 封装一个函数接收两个参数分别为行数以及列数,实现该旋涡数组。
 
思路
- 二维数组,初始填充0
- 分别记录水平和垂直方向的坐标,并根据步长更新坐标
- 如果下一步不为0,则 「拐弯」
实现
1. 根据参数生成二维数组并初始填充0
function vortex(n, m) {
  const nums = new Array(n).fill(0).map(() => new Array(m).fill(0))
  console.log(nums)
}
vortex(5, 6)

2. 分别记录水平和垂直方向的坐标,并根据步长更新坐标
这里以左上为原点
function vortex(n, m) {
  const nums = new Array(n).fill(0).map(() => new Array(m).fill(0))
  let x = 0,
    y = 0,
    stepx = 1, // 水平方向初始步长为1
    stepy = 0 // 垂直方向初始步长为0
  let count = 1
  while (1) {
    nums[x][y] = count++
    // 更新坐标
    x += stepx
    y += stepy
  }
  console.log(nums)
}
vortex(5, 6)
3. 如果下一步不为0,则 「拐弯」
 
function vortex(n, m) {
  const nums = new Array(n).fill(0).map(() => new Array(m).fill(0))
  let x = 0,
    y = 0,
    stepx = 1,
    stepy = 0
  let count = 1
  // 是否需要转弯
  function needTurn() {
    return !nums[y] || nums[y][x] !== 0 // 超出y轴或下一个位置不为0
  }
  while (count <= n * m) {
    nums[y][x] = count++
    // 更新坐标
    x += stepx
    y += stepy
    if (needTurn()) {
      // 先回到上一处
      x -= stepx
      y -= stepy
      // 转弯
      if (stepy === 0) {
        // 水平移动变为垂直移动
        stepy = stepx
        stepx = 0
      } else {
        // 垂直移动变为水平移动
        stepx = -stepy
        stepy = 0
      }
      // 更新坐标
      x += stepx
      y += stepy
    }
  }
  return nums
}
console.log(vortex(5, 6))


![某通电子文档安全管理系统 CDGAuthoriseTempletService1接口SQL注入漏洞复现 [附POC]](https://i-blog.csdnimg.cn/direct/f247ed36a4984e6ea4d242eaf454517b.png)
















