Reputation: 29
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.
Upvotes: 0
Views: 11010
Reputation: 1
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');
Upvotes: 0
Reputation: 1
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);
Upvotes: 0
Reputation: 1
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');
Upvotes: -1
Reputation: 53
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');
Upvotes: -1
Reputation: 129
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");
Upvotes: 0
Reputation: 359
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";
}
}
Upvotes: 0
Reputation: 2169
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');
Upvotes: 0
Reputation: 41
You'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');
Upvotes: 4
Reputation: 1
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.
Upvotes: 0
Reputation: 1002
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");
}
Upvotes: 0