如何修复“预期的[Array(9)]深度等于[Array(11)]”错误

时间:2019-04-12 13:37:57

标签: javascript

给出两个数字X和Y,编写一个函数: 返回X和Y之间的偶数,如果X大于Y,则返回x和y之间的奇数

预期结果:

12, 0 => [2,4,6,8,10]

实际结果:

12, 0 => [ 2, 4, 6, 8, 10 ]
0, 12 => [ 1, 3, 5, 7, 9, 11 ]

const number_game = (x, y) => {
  let numArray = [];

  if (x === 0 && y === 0) {
    return [];
  } else if (x > y) {
    for (let i = y + 1; i < x; i++) {
      if (i % 2 == 0) {
        numArray.push(i);
      }
    }
  } else {
    for (let i = x + 1; i < y; i++) {
      if (i % 2 == 1) {
        numArray.push(i);
      }
    }
  }

  return numArray;
}

console.log(number_game(12, 0));
console.log(number_game(0, 12));

0 个答案:

没有答案