Is there a more concise way to set up a conditional statement to execute for an arbitrary set of values of a variable?
本问题已经有最佳答案,请猛点这里访问。
is there /简洁/更高效的方式:eloquent to write thisP></
1 2 | else if ((aInd === 3)||(aInd === 7)||(aInd === 9)||(aInd === 19) letter = alphabet[aInd + 1].toUpperCase(); |
is that there any valid北样表P></
1 | if (a === (3 || 7 || 13 || 19) {do this} |
……或让你组sets of values of for a在单变量的方式更简明比布尔表达式与|帮of |逻辑?P></
背景:新手程序员工作的挑战,通过在线coderbyte Cypher在Basic that……以一定的动作(capitalizing if the letter is a vowel)。为简单的原因(因为这是used of the program在elsewhere)阵列declared安字母A-Z字母含each in the form of a字符串。茶是每一个transposes Cypher also to the right,the alphabet of the letters to成为vowels指数是3,7,13,19)。aind represents the index of letter in the alphabet。P></
全码:P></
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 | function letterChanges(str) { var alphabet = ['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z']; var words = str.toLowerCase().split(' '), senArray = words.map(function(word) {return word.split('')}); senArray.forEach(function(word) { word = word.filter(function(letter, index, word) { if ((Boolean(alphabet.indexOf(letter))) === false) return false; else return true; }); }); senArray[0].map(function(letter) { var aInd = alphabet.indexOf(letter); switch (aInd) { case 25: letter = alphabet[aInd].toUpperCase(); break; case (3 || 7 || 13 || 19): letter = alphabet[aInd + 1].toUpperCase(); console.log(letter); break; default: letter = alphabet[aInd + 1]; break; } return letter; }) return senArray.map(function(word) { return word.map(function(letter) { var aInd = alphabet.indexOf(letter); if (aInd === 25) {letter = alphabet[aInd].toUpperCase();} else if ((aInd === 3)||(aInd === 7)||(aInd === 13)||(aInd === 19)) {letter = alphabet[aInd + 1].toUpperCase();} else {letter = alphabet[aInd + 1];} return letter; }).join(''); }).join(' '); } letterChanges("Input string goes here."); |
P></
将值作为数组传递,并检查
在你的情况下,
1 2 3 | if ([3, 7, 13, 19].indexOf(aInd) > -1) { letter = alphabet[aInd + 1].toUpperCase(); } |