Need help in the right direction

Tell us what’s happening:
I cannot figure out a way to it seems increment or decrement the count along with getting it to print the string of “Hold” or “Bet”. What have I don’t wrong or am I missing to make this correct.

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:
    return (count += 1) ;
    break;

  case 7:
  case 8:
  case 9:
    return count;
    break;

  case 10:
  case 'J':
  case 'Q':
  case 'K':
  case 'A':
    return (count -= 1);
    break;
}
if (count = 0) {
return (count + "Bet");
} else if (count > 1) {
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');

Your browser information:

User Agent is: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_4) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/81.0.4044.138 Safari/537.36.

Challenge: Counting Cards

Link to the challenge:

1 Like
    return (count += 1) ;

Each one of these returns exits the function, meaning it will never execute any of your if statements below.

The function should return a string but instead you’re returning a number.

2 Likes

once you fix what @kerafyrm said, you have an other issue here
count = 0 is assigning 0 to count

you actually just need an if and an else, note that two of your statements do the same thing, you can merge those two
and the else is not something that you need, as you don’t want to return "Change me" in any case

3 Likes

Hey so I’ve made some changes but I’m still facing trouble any suggestions. My console.log() output turns out to be 0 still unsure whether I made changes ti fix it from exiting the function early.

var count = 0;

function cc(card) {
  // Only change code below this line
  switch(card) {
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
      return count++ ;
      break;

    case 7:
    case 8:
    case 9:
      return count;
      break;

    case 10:
    case 'J':
    case 'Q':
    case 'K':
    case 'A':
      return count--;
      break;
}
if (count == 0) {
return (count + " Bet");
} else if (count > 1 || count < 0) {
return (count + " Hold")
}
  // Only change code above this line
}
console.log(cc())
cc(2); cc(3); cc(7); cc('K'); cc('A');
1 Like

once a return statement is met, the function stops, and nothing else is executed.

Your function stops with one of these:

return count++;
return count;
return count--;

is that what you want?


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.

Please 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.

markdown_Forums

3 Likes

No of course not, I’d like for it to return a string along with the count, but I’d assumed it’d read the count then go down and include that in the parameters I’ve set with the if/else to determine whether it’s “Bet” or “Hold”.

1 Like

As soon as you hit a return, execution of the function stops. Absolutely nothing else occurs inside the function. Only return when you are done.

2 Likes

also, are you sure this is the right condition for Hold? or that you return Bet only when count equals 0?
you may want to check again challenge description

2 Likes

I figured it out for starters I was returning count too early as pointed out, I needed to fix that by 1st making a variable to assign the string "Bet" and "Hold" to a variable before the switch test. Then I needed to ensure the count was being incremented, decremented, or staying the same rather than returning the individual count stopping the entire function from flowing. Lastly, my if/else parameters were only checking to see about count with unnecessary parameters. The only parameter necessary is if count > 0 || or count < 0 . This parameter is answered in one if\else statement with if (count <= 0) else {return "Hold"}. This would also be followed by a return into the Bet local scope that would print the string should the condition be satisfied. If satisfied the strings "Bet" or “Hold” are supposed to be assigned rather than an attempt at printing the count and the string itself. Still, a bit confused as I had thought all the points would add up to a single player holding or betting. Not multiple instances all at once…

Anyways here my new code:

var count = 0;

function cc(card) {
  // Only change code below this line
  var point;
  switch(card) {
    case 2:
    case 3:
    case 4:
    case 5:
    case 6:
       count++;
      break;

    case 7:
    case 8:
    case 9:
         count == 0;
      break;

    case 10:
    case 'J':
    case 'Q':
    case 'K':
    case 'A':
        count--;
      break;
}
if (count > 0) {
point =  "Bet";
  } else {
point = "Hold";
}
return count + " " + point;

1 Like

Added my finished product. Hoping to gain a deep understanding of where I went wrong and how to fix my mistakes so that I know better for next time.

1 Like

you know, just to make it still a bit different, that could be a good place to use a ternary operator :wink:

1 Like