太多"或"语句(javascript)

mv3*_*mv3 2 javascript if-statement logical-operators conditional-statements logical-or

我正在创建一个包含6种不同问题类型的简单数学程序.我希望程序随机显示6种类型中的一种,但有些问题应该更频繁地出现.我使用加权数组,但在从加权数组中选择问题类型后,我无法在if语句中使用10个或更多"或"命令来确定问题类型.这是一个简化版本:

//shuffle array of 10 integers to get a random value
var rand_10 = [0,1,2,3,4,5,6,7,8,9];
fisherYates(rand_10);

//Set weightedProb
weightedProb[0] = probType[0];
weightedProb[1] = probType[0];
weightedProb[2] = probType[0];
.
.
.
weightedProb[8] = probType[0];
weightedProb[9] = probType[1];

theProblem = weightedProb[rand_10[0]];
if(rand_10[0] == 0 || rand_10[0] == 1 || rand_10[0] == 2 || rand_10[0] == 3 ||rand_10[0] == 4||rand_10[0] == 5||rand_10[0] == 6||rand_10[0] == 7||rand_10[0] == 8){
  //do something
}else if(rand_10[0] ==9){
  //do something else
}
Run Code Online (Sandbox Code Playgroud)

Gre*_*egL 5

一个简单的范围比较(><)是你想要的吗?

例如

if(rand_10[0] >= 0 && rand_10[0] <= 8){
  //do something
}else if(rand_10[0] == 9){
  //do something else
}
Run Code Online (Sandbox Code Playgroud)