Counting Cards: Splitting the function in 2 different functions

How would you split this long function into 2 different functions? (One function for each step: counting the card and then displaying the suggestion)
Your code so far

let count = 0;

function cc(card) {

  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";
  }

}

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/89.0.4389.82 Safari/537.36.

Challenge: Counting Cards

Link to the challenge:

I’ve edited your post for readability. When you enter a code block into a forum post, please precede it with a separate line of three backticks and follow it with a separate line of three backticks to make it easier to read.

You can also use the “preformatted text” tool in the editor (</>) to add backticks around text.

See this post to find the backtick on your keyboard.
Note: Backticks (`) are not single quotes (’).

I don’t see this as a particularly long function personally. There are a few ways to split it, but to meet the requirements, function cc(card) would have to have the same behavior, which would mean three functions total if I am counting correctly (though that depends on how splitting feels natural to you). What have you tried?

Thanks, Jeremy!
I thought 2 functions were enough, based on the task, but I can’t find out how to do it. Could you give me some hint on how you would proceed with the 3 functions? What each one of them would do?
Thank you again for your time!

Well, there are two separate tasks, updating the count, and making a string based on the count. I’d make each task a separate function and call them both from inside the cc function.

Solved!
I was doing the opposite: calling the cc function inside the diplayCount function.
Thank you for your help!
p

1 Like

Step-1: cut if else code from cc function and put it in another function like named displayResult
Step-2 : put following code outside these two functions .
let cardResult=cc(“K”);
let cardAdvice=displayResult(cardResult);
console.log(cardAdvice);

1 Like

Sure, that would work, but it wouldn’t pass the challenge that way.

1 Like

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