3.7 KiB
3.7 KiB
id, challengeType, videoUrl, forumTopicId, localeTitle
id | challengeType | videoUrl | forumTopicId | localeTitle |
---|---|---|---|---|
565bbe00e9cc8ac0725390f4 | 1 | https://scrimba.com/c/c6KE7ty | 16809 | 21点游戏 |
Description
Count Change | Cards |
---|---|
+1 | 2, 3, 4, 5, 6 |
0 | 7, 8, 9 |
-1 | 10, 'J', 'Q', 'K', 'A' |
card
的值来递增或递减变量count
,函数返回一个由当前count
和Bet
(count>0
)或Hold
(count<=0
)拼接的字符串。注意count
和"Bet"
或Hold
应该用空格分开。
例如:-3 Hold
5 Bet
提示
既然 card 的值为 7、8、9 时,count 值不变,那我们就可以忽略这种情况。
Instructions
Tests
tests:
- text: Cards Sequence 2, 3, 4, 5, 6 应该返回<code>5 Bet</code>。
testString: assert((function(){ count = 0; cc(2);cc(3);cc(4);cc(5);var out = cc(6); if(out === "5 Bet") {return true;} return false; })());
- text: Cards Sequence 7, 8, 9 应该返回 <code>0 Hold</code>。
testString: assert((function(){ count = 0; cc(7);cc(8);var out = cc(9); if(out === "0 Hold") {return true;} return false; })());
- text: Cards Sequence 10, J, Q, K, A 应该返回 <code>-5 Hold</code>。
testString: assert((function(){ count = 0; cc(10);cc('J');cc('Q');cc('K');var out = cc('A'); if(out === "-5 Hold") {return true;} return false; })());
- text: Cards Sequence 3, 7, Q, 8, A 应该返回 <code>-1 Hold</code>。
testString: assert((function(){ count = 0; cc(3);cc(7);cc('Q');cc(8);var out = cc('A'); if(out === "-1 Hold") {return true;} return false; })());
- text: Cards Sequence 2, J, 9, 2, 7 应该返回 <code>1 Bet</code>。
testString: assert((function(){ count = 0; cc(2);cc('J');cc(9);cc(2);var out = cc(7); if(out === "1 Bet") {return true;} return false; })());
- text: Cards Sequence 2, 2, 10 应该返回 <code>1 Bet</code>。
testString: assert((function(){ count = 0; cc(2);cc(2);var out = cc(10); if(out === "1 Bet") {return true;} return false; })());
- text: Cards Sequence 3, 2, A, 10, K 应该返回 <code>-1 Hold</code>。
testString: assert((function(){ count = 0; cc(3);cc(2);cc('A');cc(10);var out = cc('K'); if(out === "-1 Hold") {return true;} return false; })());
Challenge Seed
var count = 0;
function cc(card) {
// Only change code below this line
return "Change Me";
// Only change code above this line
}
// Add/remove calls to test your function.
// Note: Only the last will display
cc(2); cc(3); cc(7); cc('K'); cc('A');
Solution
var count = 0;
function cc(card) {
switch(card) {
case 2:
case 3:
case 4:
case 5:
case 6:
count++;
break;
case 10:
case 'J':
case 'Q':
case 'K':
case 'A':
count--;
}
if(count > 0) {
return count + " Bet";
} else {
return count + " Hold";
}
}