使用13个OR比较运算符编写if语句的简写方法?

时间:2014-11-04 19:52:40

标签: javascript

Roulette.prototype.setColourToNumbers = function() {
  if (this.number == 2 || this.number == 4 || this.number == 6 || this.number == 8 || this.number == 10 || this.number == 11 || this.number == 13 || this.number == 15 || this.number == 17 || this.number == 20 || this.number == 22 || this.number == 24 || this.number == 26 || this.number == 29 || this.number == 28 || this.number == 29 || this.number == 31 || this.number == 33 || this.number == 35)
  { this.colour = 'black' }
 else if (this.number == 1 || this.number == 3 || this.number == 5 || this.number == 7 || this.number == 9 || this.number == 12 || this.number == 14 || this.number == 16 || this.number == 18 || this.number == 19 || this.number == 21 || this.number == 23 || this.number == 25 || this.number == 27 || this.number == 30 || this.number == 32 || this.number == 34 || this.number == 36)
{ this.colour = 'red'}
else
{ this.colour = null}
};

我正在用Javascript编写一个程序来在轮盘上制作一个棋盘。这段代码对我来说看起来非常混乱,我想知道是否有更清晰,更短的方式来编写上述内容?有没有办法在没有指定每个数字的情况下做到这一点?就像在数组中有红色数字一样?我很难过。

感谢帮助!

7 个答案:

答案 0 :(得分:1)

我建议保留一张表格,列出哪些地图,然后在你去的地方查找价值。像这样:

var colorValues = {
  "red": [1, 3, 5, 7, 9, 12, 14, 16, 18, 19, 21, 23, 25, 27, 30, 32, 34, 36],
  "black": [2, 4, 6, 8, 10, 11, 13, 15, 17, 20, 22, 24, 26, 28, 29, 31, 33, 25]
};

function getColor(value) {
  for (var f in colorValues) {
    if (colorValues.hasOwnProperty(f) && colorValues[f].indexOf(value) > -1) {
      return f;
    }
  }

  return null;
}

// Check a few
for (var i = 0; i < 40; ++i) {
  $("body").append("<div>" + i + " is " + getColor(i) + "</div>");
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

这可以通过将每个有效输出保持为一个键,并使用一个值列表(我认为这是给定数据的最明显的结构)。收到值后,请遍历键,直到找到包含该值的键。如果没有,则返回null。

答案 1 :(得分:1)

查看How do I check if an array includes an object in JavaScript?以了解Array包含函数的一些选项。

我使用速记创建一个数字,用于解析为黑色的数字,另一个数组用于解析为红色的数字。

创建数组的简写是var redArray = [1, 3, 5, 7, ...]

答案 2 :(得分:1)

我想你想要一个switch语句。

switch(this.number) {
  case 1,2,3,4,5,6,7: this.color = 'black'; break;
  case 8,9,10,11,12,13: this.color = 'red'; break;
}

更新

此语法仅匹配系列的最后一个数字,因此没有用。

这个的开关版本是

switch(this.number) {
  case 1:
  case 2:
  case 3:
    this.color = red; break;
  case 4:
  case 5:
  case 6:
    this.color = black; break;
}

对于您的用例,这可能有点冗长。

答案 3 :(得分:0)

你走了:

Roulette.prototype.setColourToNumbers = function() {
    this.colour = null;
    var colors = {
        'red' : [2, 4, 6, 8, 10, 11, 13, 15, 17, 20, 22, 24, 26, 29, 28, 29, 31, 33, 35],
        'black' : [1, 3, 5, 7, 9, 12, 14, 16, 18, 19, 21, 23, 25, 27, 30, 32, 34, 36]
    }

    for (var k in colors) {
        if (colors[k].indexOf(this.number) > 0) {
            this.colour = k;
        }
    }
    console.log(this.colour);
};

谨防indexOf()在IE早期不起作用。

答案 4 :(得分:0)

你也可以做@ssube所建议的反转,有一组数字 - &gt;颜色。请注意使用注释来跟踪源代码中的数字到颜色。

//           0       1      2       3
var wheel= [null, "red", "black", "red", ...];

然后进行简单的查找color = wheel[this.number];

坦率地说,我更喜欢ssube的想法,但将其作为另一种选择。 YMMV。

答案 5 :(得分:-1)

&#39; ave注意到,在您的第一个if语句中,this.number == 29布尔子句被错误地复制了。

PS:我现在正在研究我的解决方案,其中包括单元测试。

答案 6 :(得分:-1)

在我写答案时,我看到'CmajSmith'提出了类似的东西。唯一的区别是我只为一次红色和黑色查找定义了数组,所以每次方法运行时我都会减少垃圾收集开销。不确定这对你来说很重要(即性能),但我认为你的方法会被频繁调用:

Roulette.prototype.setColourToNumbers = function() {
  var colour;
  if (!Roulette.prototype.setColourToNumbers.arrBlackNumbers) {
      Roulette.prototype.setColourToNumbers.arrBlackNumbers = [2, 4, 6, 8, 10, 11, 13, 15, 17, 20, 22, 24, 26, 28, 29, 31, 33, 35];
      Roulette.prototype.setColourToNumbers.arrRedNumbers = [1, 3, 5, 7, 9, 12, 14, 16, 18, 19, 21, 23, 25, 27, 30, 32, 34, 36];
  }
  colour = Roulette.prototype.setColourToNumbers.arrBlackNumbers.indexOf(this.number) > -1 ? "black" : Roulette.prototype.setColourToNumbers.arrRedNumbers.indexOf(this.number) > -1 ? "red" : null;
  this.colour = colour;
};
// *********************************************************
// U N I T    T E S T E D    M Y    A N S W E R    B E L O W
// *********************************************************
function Roulette(iNum) {
  this.number = iNum;
}
var num, roulette;
//First off unit test the 'black' numbers
num = 2, roulette = new Roulette(num);
console.assert((roulette.setColourToNumbers(), roulette.colour === 'black'), "Expected \'black\' when number is: " + num );
num = 4, roulette = new Roulette(num);
console.assert((roulette.setColourToNumbers(), roulette.colour === 'black'), "Expected \'black\' when number is: " + num);
num = 6, roulette = new Roulette(num);
console.assert((roulette.setColourToNumbers(), roulette.colour === 'black'), "Expected \'black\' when number is: " + num);
num = 8, roulette = new Roulette(num);
console.assert((roulette.setColourToNumbers(), roulette.colour === 'black'), "Expected \'black\' when number is: " + num );
num = 10, roulette = new Roulette(num);
console.assert((roulette.setColourToNumbers(), roulette.colour === 'black'), "Expected \'black\' when number is: " + num);
num = 11, roulette = new Roulette(num);
console.assert((roulette.setColourToNumbers(), roulette.colour === 'black'), "Expected \'black\' when number is: " + num);
num = 13, roulette = new Roulette(num);
console.assert((roulette.setColourToNumbers(), roulette.colour === 'black'), "Expected \'black\' when number is: " + num );
num = 15, roulette = new Roulette(num);
console.assert((roulette.setColourToNumbers(), roulette.colour === 'black'), "Expected \'black\' when number is: " + num);
num = 17, roulette = new Roulette(num);
console.assert((roulette.setColourToNumbers(), roulette.colour === 'black'), "Expected \'black\' when number is: " + num);
num = 20, roulette = new Roulette(num);
console.assert((roulette.setColourToNumbers(), roulette.colour === 'black'), "Expected \'black\' when number is: " + num );
num = 22, roulette = new Roulette(num);
console.assert((roulette.setColourToNumbers(), roulette.colour === 'black'), "Expected \'black\' when number is: " + num);
num = 24, roulette = new Roulette(num);
console.assert((roulette.setColourToNumbers(), roulette.colour === 'black'), "Expected \'black\' when number is: " + num);
num = 26, roulette = new Roulette(num);
console.assert((roulette.setColourToNumbers(), roulette.colour === 'black'), "Expected \'black\' when number is: " + num );
num = 28, roulette = new Roulette(num);
console.assert((roulette.setColourToNumbers(), roulette.colour === 'black'), "Expected \'black\' when number is: " + num);
num = 29, roulette = new Roulette(num);
console.assert((roulette.setColourToNumbers(), roulette.colour === 'black'), "Expected \'black\' when number is: " + num);
num = 31, roulette = new Roulette(num);
console.assert((roulette.setColourToNumbers(), roulette.colour === 'black'), "Expected \'black\' when number is: " + num );
num = 33, roulette = new Roulette(num);
console.assert((roulette.setColourToNumbers(), roulette.colour === 'black'), "Expected \'black\' when number is: " + num);
num = 35, roulette = new Roulette(num);
//Now unit test the 'red' numbers
console.assert((roulette.setColourToNumbers(), roulette.colour === 'black'), "Expected \'black\' when number is: " + num);
num = 1, roulette = new Roulette(num);
console.assert((roulette.setColourToNumbers(), roulette.colour === 'red'), "Expected \'red\' when number is: " + num);
num = 3, roulette = new Roulette(num);
console.assert((roulette.setColourToNumbers(), roulette.colour === 'red'), "Expected \'red\' when number is: " + num);
num = 5, roulette = new Roulette(num);
console.assert((roulette.setColourToNumbers(), roulette.colour === 'red'), "Expected \'red\' when number is: " + num);
num = 7, roulette = new Roulette(num);
console.assert((roulette.setColourToNumbers(), roulette.colour === 'red'), "Expected \'red\' when number is: " + num);
num = 9, roulette = new Roulette(num);
console.assert((roulette.setColourToNumbers(), roulette.colour === 'red'), "Expected \'red\' when number is: " + num);
num = 12, roulette = new Roulette(num);
console.assert((roulette.setColourToNumbers(), roulette.colour === 'red'), "Expected \'red\' when number is: " + num);
num = 14, roulette = new Roulette(num);
console.assert((roulette.setColourToNumbers(), roulette.colour === 'red'), "Expected \'red\' when number is: " + num);
num = 16, roulette = new Roulette(num);
console.assert((roulette.setColourToNumbers(), roulette.colour === 'red'), "Expected \'red\' when number is: " + num);
num = 18, roulette = new Roulette(num);
console.assert((roulette.setColourToNumbers(), roulette.colour === 'red'), "Expected \'red\' when number is: " + num);
num = 19, roulette = new Roulette(num);
console.assert((roulette.setColourToNumbers(), roulette.colour === 'red'), "Expected \'red\' when number is: " + num);
num = 21, roulette = new Roulette(num);
console.assert((roulette.setColourToNumbers(), roulette.colour === 'red'), "Expected \'red\' when number is: " + num);
num = 23, roulette = new Roulette(num);
console.assert((roulette.setColourToNumbers(), roulette.colour === 'red'), "Expected \'red\' when number is: " + num);
num = 25, roulette = new Roulette(num);
console.assert((roulette.setColourToNumbers(), roulette.colour === 'red'), "Expected \'red\' when number is: " + num);
num = 27, roulette = new Roulette(num);
console.assert((roulette.setColourToNumbers(), roulette.colour === 'red'), "Expected \'red\' when number is: " + num);
num = 30, roulette = new Roulette(num);
console.assert((roulette.setColourToNumbers(), roulette.colour === 'red'), "Expected \'red\' when number is: " + num);
num = 32, roulette = new Roulette(num);
console.assert((roulette.setColourToNumbers(), roulette.colour === 'red'), "Expected \'red\' when number is: " + num);
num = 34, roulette = new Roulette(num);
console.assert((roulette.setColourToNumbers(), roulette.colour === 'red'), "Expected \'red\' when number is: " + num);
num = 36, roulette = new Roulette(num);
console.assert((roulette.setColourToNumbers(), roulette.colour === 'red'), "Expected \'red\' when number is: " + num);

//Finally unit test other numbers
num = -99, roulette = new Roulette(num);
console.assert((roulette.setColourToNumbers(), roulette.colour === null), "Expected null when number is: " + num);
num = Number.POSITIVE_INFINITY, roulette = new Roulette(num);
console.assert((roulette.setColourToNumbers(), roulette.colour === null), "Expected null when number is: " + num);