Use a switch (or else if) statement to handle the different values of cards passed to the function.
Hint 2
Add/subtract the value of each card to variable count. If the card is worth 0, don’t do anything.
Hint 3
After you’ve counted the cards, use an if statement to check the value of count. Also, make sure your return has a space between the number and the string.
Solutions
Solution 1 (Click to Show/Hide)
let 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";
}
// Only change code above this line
}
cc(2); cc(3); cc(7); cc('K'); cc('A');
Code Explanation
Check the value of each card via a switch statement.
The variable count:
Increases by 1 if the card is a 2, 3, 4, 5, or 6.
Since 7, 8, and 9 aren’t worth anything, we ignore those cards in our switch statement.
Decreases by 1 if the card is a 10, ‘J’, ‘Q’, ‘K’, or ‘A’.
Check the value of count and return the appropriate response.
Example Run
cc(2); runs.
The switch statement hits case 2, jumps down and adds 1 to the variable count.
The switch statement then hits the break and cc(3); runs.
This cycle continues until the final call is made, cc('A');.
After the switch statement, the if statement checks count, which is now 0.
This then drops down to the else statement, which will return 0 Hold.
Note: As mentioned earlier, the switch statement could have also been an else if statement.
Solution 2 (Click to Show/Hide)
let count = 0;
function cc(card) {
// Only change code below this line
var regex = /[JQKA]/;
if (card > 1 && card < 7) {
count++;
} else if (card === 10 || regex.test(card)) {
count--;
}
if (count > 0) return count + " Bet";
return count + " Hold";
// Only change code above this line
}
Code Explanation
· The function first evaluates if the condition card is a value greater than 1 and lower than 7, in which case it increments count by one.
· Then if the card is 10 or higher it decrements count by one.
· The variable regex is a regular expression representing values (letters) for the higher cards.
· The else statement checks those values with the || (logical OR) operator; first for 10 and then for any string that matches the regular expression using String.match().
Thank you for this perfectly explained solution. I ended up solving the problem after the 2nd hint. Solving problems is such a rewarding feeling. I appreciate your efforts and hope to someday return the favor to others.
The interpreter will evaluate the expression after switch, in this case card. Maybe this has a value of 3, I’ll use that in my example.
Then it will evaluate the expressions after the case statements, and compare them to the value of the switch expression.
2 has a value of 2, which is not 3, so go on.
3 has a value of 3, which is 3, so from here execute code until hitting break or the end of the switch block.
Your (card >= 2 && card <= 6) returns a true or false, which does not equal any of the possible card value. You could hack it into working, using
I knew when I completed this challenge that there had got to be a simpler way of doing it… If I had left out the 7, 8 and 9 I think my earlier suggestion would’ve worked (a switch solution), but I didn’t think to remove them, and whatever I did with the count for those lines, it was messed up.
So, here is my solution. It feels a bit ridiculous to even post it, because it’s so much longer than needed. But what the heck, maybe someone will at least have a laugh checking out all the unnecessary work, lol.
[details=Code here]
var count = 0;
function cc(card) {
while (card == 2) {
count++;
break;
}
while (card == 3) {
count++;
break;
}
while (card == 4) {
count++;
break;
}
while (card == 5) {
count++;
break;
}
while (card == 6) {
count++;
break;
}
while (card == 7) {
count = count;
break;
}
while (card == 8) {
count = count;
break;
}
while (card == 9) {
count = count;
break;
}
while (card == 10) {
count--;
break;
}
while (card == "J") {
count--;
break;
}
while (card == "Q") {
count--;
break;
}
while (card == "K") {
count--;
break;
}
while (card == "A") {
count--;
break;
}
if (count >= 1) {
return count + " Bet";
} else
return count + " Hold";
}[/details]
function cc(card) {
switch(card){
case 1:
case 2:
case 3:
case 4:
case 5:
case 6:
count+=1;
break;
case 7:
case 8:
case 9:
count+=0;
break;
case 10:
case “J”:
case “Q”:
case “K”:
case “A”:
count-=1;
break;
Hi everyone,
Thank you guys for adding to the discussion of this forum.
Reading the conclusions drawn by all campers that tried to contribute to this post I see that the pattern is:
campers that used if/else to modify the value of count and then used if/else again to return count+bet/hold didn’t pass the challenge (I among those).
campers that used switch (as proposed in the official answer) or while (as proposed by @Lunaire86) in conjunction with if/else did succeed on the challenge.
Could these be true? Is it true only for this challenge? Is everyone using if/else wrong elsewhere?
Sorry this is my 3rd day programming and 2nd using JavaScript, so I genuinely have no idea, and google searches just return how to use if else on strings.
Regards
Sorry, correcting myself, it seems that the problem is actually specifying every card, as @suhassrivats solved the whole thing only recurring to if/else.
This is what I like about programming. I was stuck because I was trying to calculate the cards that had value 0! How many times do we do this real life? acknowledging things that have 0 value? Thank you so much for the explanation!! Happy coding y’all!!!
Hmm…kind of frustrating this one, maybe I just don’t have a knack for programming found a solution myself but it’s not nearly as clean…here’s mine:
var count = 0;
function cc(card) {
// Only change code below this line
var decision;
switch (card) {
case 2:
case 3:
case 4:
case 5:
case 6:
count = count + 1;
break;
case 7:
case 8:
case 9:
count = count + 0;
break;
case 10:
case “J”:
case “Q”:
case “K”:
case “A”:
count = count - 1;
break;
} if (count > 0) {
decision = “Bet”;
} else {
decision = “Hold”;
}
return (count + " " + decision);
// Only change code above this line
}
So basically I created an unnecessary extra variable and added the 7, 8, 9 cases also not needed…keep feeling I’m not cut out to be a programmer if I can’t figure out simple stuff like this…
I understood the coding part but I dont understand how does the input part work; since we have 2, 3, 4, 5, 6 here, so how would the interpreter input this data. I mean we have 5 cases here is that why we have five cc inputs. someone please help me as I am clearly not able to put my problem in words.