admin管理员组文章数量:1424893
var count = 0;
function cc(card) {
if (card <= 6){
count++;
}
else if (card >= 10){
count--;
}
else {
count += 0;
}
return count;
}
cc(2);
cc("K");
cc(7);
cc('K');
cc('A');
Writing a card counting function in JavaScript. If count <= 0
is should return count + " Hold"
. If count > 0
it should return count + " Bet"
. My issue is where do place the return so the function prints these outputs without returning and exiting the function.
var count = 0;
function cc(card) {
if (card <= 6){
count++;
}
else if (card >= 10){
count--;
}
else {
count += 0;
}
return count;
}
cc(2);
cc("K");
cc(7);
cc('K');
cc('A');
Writing a card counting function in JavaScript. If count <= 0
is should return count + " Hold"
. If count > 0
it should return count + " Bet"
. My issue is where do place the return so the function prints these outputs without returning and exiting the function.
-
It depends on where you want them returning you can simply
console.log(cc(22));
to see the output or set it to a variablevar x = cc(22)
and theconsole.log(x)
this will print it to your console output do you want it to write it to a page on insert it somewhere on your page? – Jordan Davis Commented Jan 11, 2016 at 22:18 -
return count + (count <= 0 ? " Hold" : " Bet");
– Johan Karlsson Commented Jan 11, 2016 at 22:22 - Assuming that the example code is a sample and not the actual code and that your writing an app for a browser, you output them to the log as suggested but you could use more of an AJAX approach and modify the DOM elements in question. Either way this function will eventually exit. – Lionel Morrison Commented Jan 11, 2016 at 22:24
10 Answers
Reset to default 4You're nearly there. You simply need to add in the return statements in an if...else...
at the end of the function so it will display the result after all the cards have been counted. Also for the cards to be counted correctly(blackjack rules), you have to change the else...if...
condition to else if (card <= 9)
for String("K") to be counted as -1, like so:
var count = 0;
function cc(card) {
if (card <= 6) {
count++;
} else if (card <= 9) {
count += 0;
} else {
count--;
}
if (count <= 0){
return count + " Hold"; /* Return here */
} else {
return count + " Bet"; /* and here */
}
}
cc(2); cc(3); cc(7); cc('K'); cc('A');
Create another if statement like this to return the count you want:
if (count <= 0) {
console.log(count + " Hold");
//or window.alert("Same text here");
}
else {
console.log(return count + " Bet");
//or window.alert("Same text here");
}
you can place the returns inside the if statements such as:
if (card <=6) {
count++;
return count + " Bet";
}
else if (card >= 10) {
count--;
return count + " Hold;
}
I'm still only learning, but I encountered this problem in FCC and this is how i solved it.
use the switch
var count = 0;
function cc(card) {
// Only change code below this line
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--;
break;
}
if (count > 0) {
return count + " Bet";
} else {
return count + " Hold";
}
//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');
I've just add the 0 case:
var count = 0;
function cc(card) {
// Only change code below this line
switch (card){
case 2:
case 3:
case 4:
case 5:
case 6:
count++;
break;
case 7:
case 8:
case 9:
count += 0;
break;
case 10:
case "J":
case "Q":
case "K":
case "A":
count--;
break;
}
if (count > 0){
return count + " Bet";
}else if (count<=0) {
return count + " Hold";
}
}
My answer:
LowValueCards = [2,3,4,5,6];
NoValueCards = [7,8,9];
HighValueCards = [10,"J","Q","K","A"];
if (LowValueCards.includes(card))
{
count += 1;
}
else if (NoValueCards.includes(card))
{
count += 0;
}
else if (HighValueCards.includes(card))
{
count -=1;
}
return count + (count > 0 ? " Bet" : " Hold");
This is how I solved it on FCC.
var count = 0;
function cc(card) {
// Only change code below this line
switch (card){
case 2:
case 3:
case 4:
case 5:
case 6:
count++;
break;
case 7:
case 8:
case 9:
count = count;
break;
case 10:
case "J":
case "Q":
case "K":
case "A":
count--;
break;
}
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(2); cc("J"); cc(9); cc(2); cc(7);
var count = 0;
function cc(card) {
// Only change code below this line
switch(card){
case 2:
case 3:
case 4:
case 5:
case 6:
count++;
break;
case 7:
case 8:
case 9:
break;
case 10:
case 'J':
case 'Q':
case 'K':
case 'A':
count--;
break;
}
return count>0?count + " Bet":count + " Hold";
// 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');
var count = 0;
function cc(card) {
// Only change code below this line
switch(card){
case 2:
case 3:
case 4:
case 5:
case 6:
count++;
break;
case 7:
case 8:
case 9:
break;
case 10:
case 'J':
case 'Q':
case 'K':
case 'A':
count--;
break;
}
return count>0?count + " Bet":count + " Hold";
// 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');
This is my solution, you can use the ternary operator in the return:)
var count = 0;
function cc(card) {
// Only change code below this line
switch(card){
case 2:
case 3:
case 4:
case 5:
case 6:
count++;
break;
case 7:
case 8:
case 9:
break;
case 10:
case 'J':
case 'Q':
case 'K':
case 'A':
count--;
break;
}
return count>0?count + " Bet":count + " Hold";
// 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');
var count = 0;
function cc(card) {
// Only change code below this line
switch(card){
case 2:
case 3:
case 4:
case 5:
case 6:
count++;
break;
case 7:
case 8:
case 9:
break;
case 10:
case 'J':
case 'Q':
case 'K':
case 'A':
count--;
break;
}
return count>0?count + " Bet":count + " Hold";
// 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');
本文标签: javascriptCard Counting FunctionStack Overflow
版权声明:本文标题:javascript - Card Counting Function - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745419194a2657826.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论