以下是另外兩種可以使用的方法。您可以更多地壓縮此代碼,但爲了便於閱讀,我將其展開。
對於較短的列表,您可以使用開關/外殼並使多個選項執行相同的代碼。像這樣:
thingToTest = effect("Mouth")("Slider");
switch (thingToTest) {
case 7:
case 11:
case 16:
result = 103;
break;
case 6:
case 10:
case 15:
case 25:
case 26:
result = 102;
break;
case 5:
case 9:
case 12:
case 14:
case 19:
case 24:
case 27:
case 28:
result = 101;
break;
default:
result = 100;
break;
}
問題是,如果您有很多可能的結果來檢查,可能會變得相當笨拙。在這種情況下,我會讓每個結果案例的值都是一個數組並循環遍歷它們。
thingToTest = effect("Mouth")("Slider");
mySet1 = [7, 11, 16];
mySet2 = [6, 10, 15, 25, 26];
mySet3 = [5, 9, 12, 14, 19, 24, 27, 28];
result = 100; // Default
foundIt = 0;
for (i = 0; i < mySet1.length; i++) {
if (mySet1[i] == thingToTest) {
result = 103;
foundIt = 1;
break;
}
}
if(foundIt) {
for (i = 0; i < mySet2.length; i++) {
if (mySet2[i] == thingToTest) {
result = 102;
foundIt = 1;
break;
}
}
}
if(foundIt) {
for (i = 0; i < mySet3.length; i++) {
if (mySet3[i] == thingToTest) {
result = 101;
foundIt = 1;
break;
}
}
}
result;
將連續組置於條件語句中,如果已經找到匹配,則不通過這些數組迭代來提高性能。爲了提高效率,首先測試最長的數字列表的內容是有意義的,因爲它更有可能包含匹配。
這些解決方案可能不盡如此緊湊,但絕對可擴展。