【发布时间】:2019-06-28 21:13:03
【问题描述】:
想知道是否有优化(使其更胜任)比较语法的选项。
我有一个简单的代码,它通过“if - else”条件语句检查我的函数的输入。我验证变量是否等于输入之一或不使用逻辑“或”运算符。
function cc(card) {
// Only change code below this line
if (card==2 || card==3 || card==4 || card==5 || card==6) {
count+=1;
}
else if (card==7 || card==8 || card==9) {
count+=0;
}
else if (card==10 || card=="J" || card=="Q" || card=="K" || card=="A") {
count-=1;
}
else {
return "No such combination";
}
if (count>0) {
return count + " " + "Bet";
}
else {
return count + " " + "Hold";
}
// Only change code above this line
}
// Add/remove calls to test your function.
// Note: Only the last will display
cc(7); cc(8); cc(9);
我想知道是否可以用其他语法替换这么多“OR”运算符? 我知道“切换”方法,但现在我对这种方法特别感兴趣。
【问题讨论】:
-
Argh...我不明白...似乎输入是一个数组,例如 [1, 'A', ...] 是计数操作的逻辑...但是您的代码只有一张卡......哪一张是确切的问题?
标签: javascript if-statement logical-operators