So I’m really confused by this one. Apparently I have to set the value of the cards so the “Bet 6” or whatever string is returned but I can’t seem to do that without it throwing up errors. When I try to use switch that doesn’t work either. Unless I’m supposed to write out every single card but that seems really impractical.
Can anyone help me work out what I’m supposed to be doing here?
(Please, please don’t give me answers I’m trying to work this out. Not have it solved for me.)
let count = 0;
function cc(card) {
// Only change code below this line
var ["2","3","4","5","6"] = +1;
var ["7","8","9"] = 0;
var ["10", "J","Q","K","A"] = -1;
switch (card){
case 2,3,4,5,6:
return "Bet";
break;
case 7,8,9:
return "Hold";
break;
case 10,"J", "Q","K","A":
return "Hold";
break;
}
return "Change Me";
// Only change code above this line
}
cc(2); cc(3); cc(7); cc('K'); cc('A');
**Your browser information:**
User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/605.1.15 (KHTML, like Gecko) Version/13.1 Safari/605.1.15
Don’t worry about coming up with the cleanest solution right now.
Just focus on solving the problem even if it is a little messy.
Then once you solve it, you can go back and refactor it.
Or you can share your solution in the forum wrapped in spoiler tags [spoiler] and ask the forum for help on optimizing your code.
This will come in handy when you start working through the basic and intermediate algorithms.
I have this now. I’m trying to use switch to check the card and increase and decrease the count. Not sure if I’m on the right track. I’m trying to use the if/else to put together the string it wants but that won’t work for some reason.
let count = 0;
function cc(card) {
// Only change code below this line
switch (count){
case 2:
return +1;
break;
case 3:
return +1;
break;
case 4:
return +1;
break;
case 5:
return +1;
break;
case 6:
return +1;
break;
case 7:
return 0;
break;
case 8:
return 0;
break;
case 9:
return 0;
break;
case 10:
return -1;
break;
case "J":
return -1;
break;
case "Q":
return -1;
break;
case "K":
return -1;
break;
case "A":
return -1;
break;
}
if (card > 0) {
return "Bet" + count;
}
else if {
return "Hold" + count;
}
else {
return "Change Me";
// Only change code above this line
}
cc(2); cc(3); cc(7); cc('K'); cc('A');
Ah I think given I’ve forgtton those lessons entirely I’m going to also go though the whole course again to this point. Just to try to get things to stick more.
Okay now I have this. It still doesn’t work. I’m really really unsure where to go from this.
let count = 0;
function cc(card) {
// Only change code below this line
switch(card){
case 1:
count++;
break;
case 2:
count++;
break;
case 3:
count++;
break;
case 4:
count++;
break;
case 5:
count++;
break;
case 6:
count++;
break;
case 7:
count +0;
break;
case 8:
count +0;
break;
case 9:
count +0;
break;
case 10:
count--;
break;
case "J":
count--;
break;
case "Q":
count--;
break;
case "K":
count--;
break;
case "A":
count--;
break;
}
if (count >0){
return "Bet " + count;
}
else if (count ==0){
return "Hold " + count;
}
else if (count < 0){
return "Hold " + count;
}
else{
return "Change Me";
// Only change code above this line
}
}
cc(2); cc(3); cc(7); cc('K'); cc('A');
The only semantic mistake is: Example Outputs:-3 Hold or 5 Bet // first count goes and then the word
Also, a few notes about optimization:
let count = 0;
function cc(card) {
switch (card) {
// switch has so-called "fall through" functionality
// so if you want to execute the same block of code under a few
// conditions you may write like:
// switch (val) {
// case 1:
// case 2:
// case 3:
// doSmething();
// break;
// case 4:
// case anything:
// do();
// break;
// }
case 1:
count++;
break;
case 2:
count++;
break;
case 3:
count++;
break;
case 4:
count++;
break;
case 5:
count++;
break;
case 6:
count++;
break;
// check for 7, 8, 9 is unnecessary as we do nothing with count
case 7:
count + 0;
break;
case 8:
count + 0;
break;
case 9:
count + 0;
break;
case 10:
count--;
break;
case "J":
count--;
break;
case "Q":
count--;
break;
case "K":
count--;
break;
case "A":
count--;
break;
}
// you may simplify this if/else if/else if/else statement
if (count > 0) {
return count + " Bet";
}
// the following conditions are redundant, because we already know
// the count here will be less than or equal to zero because we have checked it already
// in the first if (count > 0)
else if (count == 0) {
return count + " Hold";
}
else if (count < 0) {
return count + " Hold";
}
else {
return "Change Me";
// Only change code above this line
}
}
cc(2); cc(3); cc(7); cc('K'); cc('A');
let count = 0;
function cc(card) {
// Only change code below this line
switch(card){
case 1:
count++;
break;
case 2:
count++;
break;
case 3:
count++;
break;
case 4:
count++;
break;
case 5:
count++;
break;
case 6:
count++;
break;
case 10:
count--;
break;
case "J":
count--;
break;
case "Q":
count--;
break;
case "K":
count--;
break;
case "A":
count--;
break;
}
if (count >0){
return count + "Bet ";
}
else if (count <0){
return count + "Hold ";
}
else{
return "Change Me";
}
// Only change code above this line
}
cc(2); cc(3); cc(7); cc('K'); cc('A');
And it still doesn’t work. Is there somewhere I’m going wrong I’m majorly missing?
// running tests Cards Sequence 7, 8, 9 should return the string
0 Hold
// tests completed
let count = 0;
function cc(card) {
// Only change code below this line
switch(card){
case 1:
count++;
break;
case 2:
count++;
break;
case 3:
count++;
break;
case 4:
count++;
break;
case 5:
count++;
break;
case 6:
count++;
break;
case 10:
count--;
break;
case "J":
count--;
break;
case "Q":
count--;
break;
case "K":
count--;
break;
case "A":
count--;
break;
}
if (count >0){
return count + " Bet";
}
else if (count <0){
return count + " Hold";
}
else{
return "Change Me";
}
// Only change code above this line
}
cc(2); cc(3); cc(7); cc('K'); cc('A');