Counting Cards: bet or hold

Counting Cards: bet or hold
0

#1

Tell us what’s happening:
I must be missing a syntax somewhere. What am I doing wrong?

Your code so far


var count = 0;
var str ="";

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

  case 7:
  case 8:
  case 9:
  break;
  case 10:
  case "J":
  case "Q":
  case "K":
  case "A":
  count--;
  break;
  }
}

str += "+count+";
  if (count>0){
    str += " Bet";
    return str;
  } else if (count<=0){
    str += " Hold";
    return str;
  }

  // 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');

Your browser information:

User Agent is: Mozilla/5.0 (Linux; Android 7.0; LGMS210 Build/NRD90U) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.109 Mobile Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/counting-cards/


#2

First, you closed out your function with } too early.

Also, that global str variable is going to cause a problem. Move it inside your function.

Because str is global and the FCC tests run one after the other, each time the function is called, str starts with the same value it last ended.


#3

I turned the Str section into a new function. Then I call the function runStr() in each sectio.n of whether to bet or hold. However this is not working. What do you think is wrong?


#4

Can you post your new code?


#5

Tell us what’s happening:
I’m calling the str steing using runStr() function. But the system isn’t taking it. What’s wrong?

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++;
  runStr();
  break;

  case 7:
  case 8:
  case 9:
  runStr();
  break;
  case 10:
  case "J":
  case "Q":
  case "K":
  case "A":
  count--;
  runStr();
  break;
  }
}

function runStr(){
var str="";
str += "+count+";
  if (count>0){
    str += " Bet";
    return str;
    break;
  } else if (count<=0){
    str += " Hold";
    return str;
    break;
  }
}
  // 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');

Your browser information:

User Agent is: Mozilla/5.0 (Linux; Android 7.0; LGMS210 Build/NRD90U) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.109 Mobile Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/counting-cards/


#6

You have a couple of issues. You can not use the break statement unless you are in a loop. Your runStr function does not have a loop, so it errors out when it hits these lines.

Also, your main function cc does not explicitly return a value (you are not using a return statement), so your function returns undefined.

Let’s assume you figure how to return a value from your cc function using the return value of the calls to runStr located 3 places in your code. Your runStr function will always return the same string:

“+count+ Bet”

You need to review how to correctly concatenate variables with strings. See the previous challenge below for how to do this.

https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/constructing-strings-with-variables


#7

Sorry. I am not too clear on what you suggest is the solution to this card counting assignment.


#8

Tell us what’s happening:
Ok. I made some modifications based on your feedback. I am not clear what you mean by returning value from cc or from runStr(). Can you pls be more clear?
Something about movimg a variable/parameter from one into the other.and getting a return on it but I’m not clear what or how or what this looks like?

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++;
  runStr();
  break;

  case 7:
  case 8:
  case 9:
  runStr();
  break;
  case 10:
  case "J":
  case "Q":
  case "K":
  case "A":
  count--;
  runStr();
  break;
  }
}

function runStr(){
var str="";
str += count;
  if (count>0){

    str += " Bet";
    return str;
  } else if (count<=0){
    str += " Hold";
    return str;

  }
}
  // 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');

Your browser information:

User Agent is: Mozilla/5.0 (Linux; Android 7.0; LGMS210 Build/NRD90U) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/65.0.3325.109 Mobile Safari/537.36.

Link to the challenge:
https://learn.freecodecamp.org/javascript-algorithms-and-data-structures/basic-javascript/counting-cards/


#9

Your runStr has a return statement (actually two), but your cc function does not have a return statement to return anything. By default, JavaScript returns undefined when a function does not explicitly return a value. The cc function neds to return the final string of count + " Bet" or " Hold".

Your runStr function returns the correct final string back to cc, but cc is not doing anything with that string. It should be returning it back to the calling function.