It also works with
count += 1
and
count -= 1
Thanks a lot. It helped.
My initial solution is nearly identical to the example (I was shocked!). It passes all of the tests when I enter the numbers, but shows straight fails when I run it. Why?
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 + " Hold";
} else {
return count + " Bet";
}
Ok so please tell me…I decided to do my cases like this
case (2||3||4||5||):
Now, it ran and worked with (7||8||9) but almost nothing else. Why?
This was my code that worked:
function cc(card) {
// Only change code below this line
if (card >= 2 && card <= 6){
count++;
} else if (card == 10 || typeof card === 'string'){
count--;
}
if(count > 0){
return(count+" Bet");
}else{
return(count+" Hold");
}
// Only change code above this line
}
Nice guideline, was able to solve this issue, with switch
I think you just have bet and hold backwards, this is otherwise identical to what I used as well.
if (count < 1) {
return count + " Hold";
}
else return count + " Bet";
From what I could understand trying to get cases to work, it seems comparisons aren’t possible?
case ( >=2 && <=7) {count ++;break;}
I couldn’t get code like above to work, only with:
case 2: case 3: case 4: etc.
Is it possible to use those comparisons, or is that why people are finding if/else if easier?
This are more elegant solution, but for novice like me I did not know I can only compare “typeof” mentioned by shikkaba
so here is my solution
if (card <=6){
count++;
}
else if (card>=7 && card<=9){
}
else {
count--;
}
if(count>0){
return count + " Bet";
}
else {
return count + " Hold";
}
above code will give garbage result if card input is less than 2 or greater than 9 but it passed the test.
or you can omit the (card >= 7) because above it there’s (card <= 6) already. so you code will be like this
var count = 0;
function cc(card) {
if (card <= 6) ++count;
else if (card <= 9); // do nothing to count if card is either 7, 8 or 9
else --count; // all the other cases is either 10, “J”, “Q”, “K”, “A”
if (count > 0){
return count + " Bet";
} else {
return count + " Hold";
}
}
why does this loop over after it checks the first value? shouldn’t it break out of the loop after it hits the first case it matches and proceed to check the value of count in the if statement?
Can someone explain why this doesn’t work?
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 = count++ + " Bet";
break;
case 7:
case 8:
case 9:
count = 0 + " Hold";
break;
case 10:
case “J”:
case “Q”:
case “K”:
case “A”:
count = count-- + " Hold";
break;
}
return count;
I want to second this question. I had something similar and I wonder if anyone knows why doesn’t it work:
function cc(card) {
// Only change code below this line
switch (card) {
case 2:
case 3:
case 4:
case 5:
case 6:
count += +1;
console.log = (count + " Bet");
break;
case 7:
case 8:
case 9:
count += 0;
console.log = (count + " Hold");
break;
case 10:
case 'J':
case 'Q':
case 'K':
case 'A':
count -= -1;
console.log = (count + " Hold");
break;
Could someone please explain why this code isn’t passing? All of the cases pass except for “3,7,Q,K,A” and I’m so confused
case 2,3,4,5,6:
return "5 Bet";
case 7,8,9:
return"0 Hold";
case 10,"J","Q","K","A":
return "-5 Hold";
case 3,7,"Q","K","A":
return "-1 Hold";
case 2,"J",9,2,7:
return"1 Bet";
case 2,2,10:
return "1 Bet";
case 3,2,"A",10,"K":
return "-1 Hold";
}
Took me so long, but i did it! Click on blur
[spoiler]var count = 0;
function cc(card) {
// Only change code below this line
var answer="";
switch(card){
case 2:
case 3:
case 4:
case 5:
case 6:
answer = count;
count++;
break;
case 7:
case 8:
case 9:
answer=count;
break;
case 10:
case ‘J’:
case ‘Q’:
case ‘K’:
case ‘A’:
answer = count;
count–;
break;
}
if (answer>0){
return count + " Bet";
}else
return count + " Hold";
// Only change code above this line
// 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’);
[/spoiler]
Well, I did the same mistake at the start Try to read above Hint 1, Hint 2 and Hint 3. So first Hint 1 we need to count the value of each card, for example:
switch(val){ //val is just an example
case X:
case Y:
case Z:
}
Than you need to add/sub (just like it says on Hint 2) to variable count(in ex.num), for example adding(+):
case X:
case Y:
case Z:
num++;
break;
Or Ex for Sub(-)
case X:
case Y:
case Z:
num–;
break;
When you finish with each of the cards, than we need to use the Hint 3. So for example:
if(num>0){
return num + " hello"
}else{
return num + " Bye"
Let me know if it helps .
I tried running your code and I think I know Why. I did almost the same way, but just in case added 7, 8, 9 (even though it’s says extra). Try adding cards 7, 8, 9 so it will be like this:
var count = 0;
function cc(card) {
var answer=“”;
switch(card){
case 2:
case 3:
case 4:
case 5:
case 6:
answer = count;
count++;
break;
case 7:
case 8:
case 9:
answer=count;
break;
case 10:
case ‘J’:
case ‘Q’:
case ‘K’:
case ‘A’:
answer = count;
count–;
break;
}
if (count>0){
return count + " Bet";
}else
return count + " Hold";
}
looks like you have two mistakes with :
count += +1; // should be count++; or count–; or just count;
console.log = (count + " Bet");// you should declare in a different if else statement.
break;
I agree, this example and explanation was by far the best I have seen. I wish the instructions for the exercises (challenges) were better explained. sometimes I feel like I know what I want to do but am unsure which operation I should use (if statement, switch, both) this is the only thing that is drawing me back on here. Thanks for the info.
Everything Works except this case 3,7,‘Q’,8,‘A’:
i dont understand why.
function cc(card) {
var count = 0;
// Only change code below this line
switch (card){
case 2:
count = “5 Bet”;
break;
case 3:
count = “5 Bet”;
break;
case 4:
count = “5 Bet”;
break;
case 5:
count = “5 Bet”;
break;
case 6:
count = “5 Bet”;
break;
case 7,8,9:
count = “0 Hold”;
break;
case 10,‘J’,‘Q’,‘K’,‘A’:
count = “-5 Hold”;
break;
case 3,7,‘Q’,8,‘A’:
count = “-1 Hold”;
break;
case 2,‘J’,9,2,7:
count = “1 Bet”;
break;
case 2,2,10:
count = “1 Bet”;
break;
case 3,2,‘A’,10,‘K’:
count = “-1 Hold”;
break;
}
return count;
// Only change code above this line
}
// Add/remove calls to test your function.
// Note: Only the last will display
cc(3); cc(7); cc(8); cc(‘Q’); cc(‘A’);
My take:
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 += 1;
break;
case 7:
case 8:
case 9:
break;
case 10:
case 'J':
case 'Q':
case 'K':
case 'A':
count -= 1;
break;
}
if(count > 0) {
return +count+ " Bet";
}
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(3); cc(7); cc(‘K’); cc(‘A’);