JavaScript偶数和奇数范围

时间:2018-10-02 20:01:06

标签: javascript arrays range

我正在尝试解决在线测验,但我似乎无法通过所有测试。这是问题

给出两个数字X和Y,编写一个函数: 1返回X和Y之间的偶数,如果X大于Y,否则返回x和y之间的奇数 例如,取整数10和2。该函数将返回2到10之间的所有偶数。

示例:

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

2,12 => [3,5,7,9,11]

0,0 => []

这是我的代码:

    function number_game(x, y){
      let numbers = [];

      if (x > y){
        for (let i = y; i <= x; i++){
          if (i > y){
            numbers.push(i);
          }
        }
      }else{
        for (let i = x; i <= y; i++){
          if (i > x){
            numbers.push(i);
        }
      }
    }

      const result = numbers.filter(function(num){
        return x > y ? num % 2 === 0: num % 2 === 1;
      });
      return result;
  }

6 个答案:

答案 0 :(得分:2)

因为我太喜欢代码高尔夫了!

const number_game = (x, y) => {
  const min = Math.min(x, y), max = Math.max(x, y);

  return Array.from(Array(max - min), (_, i) => i + min).slice(1)
    .filter(v => v % 2 == (x < y));
};

答案 1 :(得分:1)

虽然不是最佳编写方式,但是您的代码本质上是可以的,只是它的结果中包含更高的数字。您可以通过if (i > y)测试跳过较低的数字,尽管仅从y + 1开始循环会比较简单。

要排除较高的数字,只需将重复标准从<=更改为<

在这些循环中执行偶数或奇数测试也将更加简单。

function number_game(x, y) {
  let numbers = [];

  if (x > y) {
    for (let i = y + 1; i < x; i++) {
      if (i % 2 == 0) {
        numbers.push(i);
      }
    }
  } else {
    for (let i = x + 1; i < y; i++) {
      if (i % 2 == 1) {
        numbers.push(i);
      }
    }
  }

  return numbers;
}

console.log(number_game(12, 0));
console.log(number_game(2, 12));
console.log(number_game(0, 0));
console.log(number_game(3, 13));
console.log(number_game(1, 1));

答案 2 :(得分:0)

也许这样的事情可能会有所帮助。

db.stock.aggregate({
    $facet: {
        ops: [ { $match: {} } ]
    }
})

答案 3 :(得分:0)

除了生成所有数字然后进行过滤之外,您还可以生成所需的数字:

function number_game(x, y) {  
  const start = Math.min(x, y);
  const end = Math.max(x, y);
  const base = x > y ? 2 - start % 2 : start % 2 + 1; // how much you need to add, to get from start to the first number in the result
  const numbers = [];
  
  for(let i = start + base; i < end; i+= 2) numbers.push(i);
  
  return numbers;
}

console.log(JSON.stringify(number_game(9, 1)));
console.log(JSON.stringify(number_game(1, 9)));
console.log(JSON.stringify(number_game(12, 2)));
console.log(JSON.stringify(number_game(2, 12)));
console.log(JSON.stringify(number_game(12, 1)));
console.log(JSON.stringify(number_game(1, 12)));
console.log(JSON.stringify(number_game(2, 2)));

答案 4 :(得分:0)

function returnOddOrEven(x,y){

// return empty array if both x and y are equal to 0

let mixedArr = [];
if (x ===0 && y===0){
   return [];
}


// first condition of x greater than y
else if ( x > y){
   for (var i = 1; i < x; i++){
         if( i % 2 === 0){
           mixedArr.push(i)
         }
   }
   }

   // second condition of y > x
   else if( y > x){
     for (var i = 1; i < y; i++){
         if(i > 1 && i % 2 === 1){
           mixedArr.push(i)
         }
   }

}

return mixedArr;
} 

答案 5 :(得分:0)

function number_game(x, y) {
    var numArray = new Array();
    if (x > y) {
        for (i=y+1; i<x; i++) {
            if (i%2 == 0) {
                numArray[numArray.length] = i;
            }
        }
    } else {
        for (i=x+1; i<y; i++) {
            if (i%2 != 0) {
                numArray[numArray.length] = i;
            }
        }
    }
    return numArray;
}