Can some1 explain this in detail with example?

Counting Cards
no.80 of JavaScript I guess

var count = 0;

function cc(card) {
// Only change code below this line

return "Change Me";
// Only change code above this line

cc(2); cc(3); cc(7); cc('K'); cc('A');

can some1 explain this in detail that so i can understand it easily
i dont want to do copypaster cuz i know that would be so bad for my own future!

can you tell us what you do not understand?

you have a global count variable that you need to update,
your function receives a card based on which you need to update the global variable,
and the output of the function is based on the global variable

The idea is such as you have a variable which is counting up or down based on each given input into the function. Then the function returns a string with the current count and a word based on if the count is bove 0 or not.

Imagine you got an “add” function, which takes in a value and adds it to a global count. Then it returns the current count as well as positive/negative based on it’s sign.

So add(1), add(-3), add(5)
will return:
“1 positive”, “-2 negative”, “3 positive”
You obviously need a global count, to keep track of the previous values. Within the function you can simply reference count, because as a global variable, it will be available no problem. It really comes down just to writing the checks for the card-values given in the table.

Also if you are irritated by the expected outputs: Those are the FINAL output of the last function call. As in the example, the function is called several times and always has a return value - the test hwoever only mentions the final one.

can u tell me what are the following
count > 0
and what are they for?
what does they do?

can u tell me how does it happened and was its useful to add then in the code?

let count = 0;

function cc(card) {
  // Only change code below this line
  switch (card) {
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
    case 10:
    case "J":
    case "Q":
    case "K":
    case "A":
  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');

This code uses a special weirdness of switch-case. It will check the values of card in every case and once a case is found, it executes ALL the following cases up until it hit’s a break.
Usually this is annoying as you have to write a break into every case. Here however it’s helpful as for case 2, 3, 4, 5 and 6 you want to do the same. So if card=3, it will start in case 3 and execute the code for the following cases. Those are empty up until case6, where it increments count and exits the switch-case.

count++ is called an increment. It’s short for count = count +1.
count-- is decrement and decreases the value by 1.
count > 0 is a condition and will return either True or False depending on the value of count. Within an if-else, the True means the if-case is executed, the False executes the else.

oh! thanks alot for helping me @Jagaya

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.