检查变量相等性的语法
The syntax for checking equality of a variable
想知道是否有优化(使其更胜任)比较语法的选项。
我有一个简单的代码,它通过 "if - else" 条件语句检查函数的输入。我使用逻辑 "OR" 运算符验证变量是否等于输入之一。
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" 运算符?
我知道 "switch" 方法,但现在我对这种方法特别感兴趣。
您可以使用 Array.prototype.includes
:
if ([2, 3, 4, 5, 6].includes(card)) {
count += 1;
} else if ([7, 8, 9].includes(card)) {
count += 0;
} else if ([10, "J", "Q", "K", "A"].includes(card)) {
count -= 1;
}
if (card==2 || card==3 || card==4 || card==5 || card==6) {
count+=1;
可以改成
if (card >= 2 && card <= 6) {
count+=1;
编辑:
如果你只想要整数,你可能想要添加 card / 2 == 0
.
您可以使用一个对象将卡值映射到要添加到 count
的金额:
const cards = {
2: 1, 3: 1, 4: 1, 5: 1, 6: 1,
7: 0, 8: 0, 9: 0,
10: -1, J: -1, Q: -1, K: -1, A: -1
};
function cc(card) {
if (card in cards) {
count += cards[card];
if (count > 0) {
return count + " Bet";
} else {
return count + " Hold";
}
} else {
return "No such combination"
}
}
你可以这样写,这样可读性更好
function cc(cards) {
let all = [
{ cards : [2,3,4,5,6] : value : 1},
{ cards : [7,8,9] : value : 0},
{ cards : [10,'J','Q','K','A'] : value : -1}
];
return cards.reduce( (acc,card) => {
let found = all.find( v => v.cards.indexOf(card) !== -1 );
if ( !found ) throw 'No such combination';
return acc + found.value;
}, 0);
}
抱歉编辑,我刚刚意识到输入是一个数组...
你可以把 throw/hold 测试放在这个函数之外:)
想知道是否有优化(使其更胜任)比较语法的选项。
我有一个简单的代码,它通过 "if - else" 条件语句检查函数的输入。我使用逻辑 "OR" 运算符验证变量是否等于输入之一。
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" 运算符? 我知道 "switch" 方法,但现在我对这种方法特别感兴趣。
您可以使用 Array.prototype.includes
:
if ([2, 3, 4, 5, 6].includes(card)) {
count += 1;
} else if ([7, 8, 9].includes(card)) {
count += 0;
} else if ([10, "J", "Q", "K", "A"].includes(card)) {
count -= 1;
}
if (card==2 || card==3 || card==4 || card==5 || card==6) {
count+=1;
可以改成
if (card >= 2 && card <= 6) {
count+=1;
编辑:
如果你只想要整数,你可能想要添加 card / 2 == 0
.
您可以使用一个对象将卡值映射到要添加到 count
的金额:
const cards = {
2: 1, 3: 1, 4: 1, 5: 1, 6: 1,
7: 0, 8: 0, 9: 0,
10: -1, J: -1, Q: -1, K: -1, A: -1
};
function cc(card) {
if (card in cards) {
count += cards[card];
if (count > 0) {
return count + " Bet";
} else {
return count + " Hold";
}
} else {
return "No such combination"
}
}
你可以这样写,这样可读性更好
function cc(cards) {
let all = [
{ cards : [2,3,4,5,6] : value : 1},
{ cards : [7,8,9] : value : 0},
{ cards : [10,'J','Q','K','A'] : value : -1}
];
return cards.reduce( (acc,card) => {
let found = all.find( v => v.cards.indexOf(card) !== -1 );
if ( !found ) throw 'No such combination';
return acc + found.value;
}, 0);
}
抱歉编辑,我刚刚意识到输入是一个数组... 你可以把 throw/hold 测试放在这个函数之外:)