I don’t understand this lesson, I need help understanding what is this code like what is the logic, how it’s used, and how to pass this lesson.
Your code so far
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
case
case
count --;
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 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.122 Safari/537.36.
From what I see in your code, you’re almost there. Although you should always have a break statement inside each case statement, for this case however, it’s a trick you can use by NOT putting those break statements. It does work because I remember the way I had implemented this exercise was similar to yours.
Then at the end of your function, you will have something like this:
if (count == 0) {
return count+ " Hold";
} else if ( count > 1) {
return ...
// and so on
}
Theoretically you should. However in this case, you can “cheat”. And by that I mean:
switch(card) {
case 2: // all these
case 3: // cases share
case 4: // the same set
case 5: // of operations.
case 6: // Which is: increment by 1 when you get 2, 3, 4, 5 or 6.
count ++; // here is your increment operation
break; // and here is your break, making sure to stop only if you get one of those values above
case
case
case
count --;
break;
}
The orthodox programmers would tell you that that piece of code looks horrible and you should never do such thing.
Okay, here is the correct alternative to that dodgy code:
switch(card) {
case 2:
count++;
break;
case 3:
count++;
break;
case 4:
count++;
break;
case 5:
count++;
break;
case 6:
count++;
break;
case
case
case
count--;
break;
}
Now tell me, which one has fewer lines and comes up with the same result? Like I said about the previous code, you should avoid NOT using break inside of a case statement because you can easily lose control of you logic and outcomes. But for such simple function, coutindCards, you can think ouside of the box a bit and do the dodgy way sometimes. So, it’s up to you to decide which way to go hahaha
This is what I have inside the comments, somethings are wrong, can tell me what?
switch(card) {
case 2:
count++;
break;
case 3:
count++;
break;
case 4:
count++;
break;
case 5:
count++;
break;
case 6:
count++;
break;
case 7:
case 8:
case 9:
count--;
break;
Ok this is the solution how can I practice more with it?
switch(card) {
case 2:
count++;
break;
case 3:
count++;
break;
case 4:
count++;
break;
case 5:
count++;
break;
case 6:
count++;
break;
case 10:
case "J":
case "Q":
case "K":
case "A":
count--;
break;