Counting Cards_02

Counting Cards_02
0

#1

Tell us what’s happening:
I’m having trouble figure out the logic for this lesson.

Your code so far

var count = 0;

function cc(card) {
  // Only change code below this line
  if(card>=2 && card <=6){
    count++;
    return count + " Bet";
  }
  else if(card>=7||card<=9){
  return count + " Hold";
}
  else if (card==10||card=="J"||card=="Q"||card=="K"||card=="A"){
    count--;
    return count + " Hold";
  }
  
  return "change me" ;
  // 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’);

// running test
Cards Sequence 10, J, Q, K, A should return -5 Hold
Cards Sequence 2, J, 9, 2, 7 should return 1 Bet
Cards Sequence 2, 2, 10 should return 1 Bet
Cards Sequence 3, 2, A, 10, K should return -1 Hold
// tests completed
Your browser information:

User Agent is: Google Chrome Version 68.0.3440.75 (Official Build) (64-bit)

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


#2

Try separating the logic which increases or decreases the value of count from what actually gets returned. The card value is the only thing which should increase or decrease the value of the count variable. Also, the value of count should be the only thing which determines the string which gets returned.


#3

I tried that., but when I put a return statement below the conditional logic it returns values each time a function is called. For example:

var count = 0;

function cc(card) {
  // Only change code below this line
  if(card>=2 && card <=6){
    count++;
  }

  else if (card==10||card=="J"||card=="Q"||card=="K"||card=="A"){
    count--;
  }
  if(count>=2 && count <=6){
return count + " Bet";
 }
else if (count>=7 && count<=9){
return count + " Hold";
}

and so on. The above code will return each time the function is called which is undesirable.


#4

The above line returns count + “Bet” only if count >= 2 and count <=6. Count could be any value between 0 and Infinity. The instructions tell you the following:

The function will then return a string with the current count and the string Bet if the count is positive, or Hold if the count is zero or negative.

Why are you basing your return value on count values only between 2 and 6 (inclusive) OR values between 7 and 9 (inclusive)? To return count + " Bet" you only care if count is greater than 0. Otherwise, you should return count + " Hold".


#5

This is what I have so far. It still doesn’t work because the last number in the test leaves from the “Hold” part of the program. I’m still confused how to set this up.

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 10:
    case "J":
    case "Q":
    case "K":
    case "A":
    count--;
    break;
  }
  if(card>=2 && card <=6){
    return count + " Bet";
  }
  
  else if((card>=7 && card<=9)||(card==10 || card=="J" || card=="Q" || card== "K" || card== "A")){
    return count + " Hold";
  }
  
  return "Change Me";
  // 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’);

// running test
Cards Sequence 2, J, 9, 2, 7 should return 1 Bet
Cards Sequence 2, 2, 10 should return 1 Bet
// tests completed


#6

i believe this should be count- -?

edit: or maybe it is and I just can’t see…


#7

It is count–: (two dashes)


#8

these ifs are interesting…

should you be looking at count not card?
As the count determines whether to hold or bet?

Edit : from the instructions:

The function will then return a string with the current count and the string Bet if the count is positive, or Hold if the count is zero or negative.


#9

I’ve edited your post for readability. When you enter a code block into the forum, precede it with a line of three backticks and follow it with a line of three backticks to make easier to read. See this post to find the backtick on your keyboard. The “preformatted text” tool in the editor (</>) will also add backticks around text.


#10

Okay, thanks for the info.


#11

Yeah, I’m lost on this. I tried replacing card with count in the if statements, but I received more errors than I have now with this code.


#12

my friend, just try reading the instructions a little bit more carefully.
It is not as simple as replacing a variable.

The function will then return a string with the current count and the string Bet if the count is positive, or Hold if the count is zero or negative .

#13

If you read my last reply to you (see below), I said were basing what gets returned on the card value and you should not be doing that.

In your newly post code, you are still basing your return logic on the card value. What gets returned has nothing to do with the current card value passed into the function. What gets returned has everything to do with the current count value. You only need one if statement and an else statement (not else if) for the code at the bottom which deals with what to return.


#14

You’re right. I put the following at the bottom:

if(count>0){
return count + " Bet";
else{
return count + " Hold";

Sometimes it’s hard to follow instructions. Sometimes that’s the hardest part. Thanks for all your help!!