Card Counting JavaScript

Hello, I am trying to make the card counting function in Javascript. The lesson prescribes using the switch function, and that works just fine when I try it. However I initially started with if and else if statements. I really would like to get that to work.

My problem is that console.log only shows positive or negative integers. It will only either add or subtract and not both.

  **Your code so far**

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');
  **Your browser information:**

User Agent is: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/92.0.4515.159 Safari/537.36

Challenge: Counting Cards

var count=0;

function cc(card) {

//plus 1
  if (card=2) {
       count=count+1;

    }

    else if (card=3) {
      count=count+1;

    }

    else if (card=4) {
      count=count+1;

    }

    else if (card=5) {
      count=count+1;

    }

    else if (card=6) {
      count=count+1;

    }

    // no value

    //-1

    else if (card=10) {
      count-1;

    }

    else if (card='J') {
      count=count-1;

    }

    else if (card='Q') {
      count=count-1;

    }
    else if (card='K') {
      count-1;

    }

    else if (card='A') {
      count=count-1;

    }
}


cc('3'); cc('3');
console.log(count);

You can’t use a single = to check for equality. You should use a triple ===.

You probably also want to combine cases so you are not repeating the same code over and over.

1 Like

Thanks for your response. Changing to the === yielded the same results.

When I call cc(2); cc(3); cc(‘K’); cc(‘K’); cc(‘A’); I expect that my var count should be changed to -1. Since the functions should add +1 +1 -1-1-1 respectively. When I log the var count the result is 5.

Here is my retry at the code. Just the if and else if statement. When I trade the reverse the statements (negative value for the if and positive for the if else) I get -5 as my results

  if  (card===2,3,4,5,6) {
     count++;
  }

  else if (card===10,'K','Q','J','A') {
    count--;
  }

I am going to start the set of lessons I am clearly missing something fundamental here :sob:

I don’t see where you’re adding the Hold or Bet part and returning a string.

This is not a valid test to see if the card is one of multiple values.

var count = 0;

function cc(card) {
 

    if (card=2,3,4,5,6) {
         count++;
      }
      else if (card=10,'K','Q','J','A') {
        count--;
      }
      if (count > 0) {
         return count + " Bet";
       } else {
         return count + " Hold";
       }
  }
cc(2); cc(3); cc('K'); cc('K'); cc('A');
console.log(count);

You went back to a single equal sign, which is still not a comparison operator. Also, this is still not a valid way to check for multiple values.

You should make separate checks for equality separated by logical OR operators.

I’ve edited your posts 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 (’).

Played around with the or operator and went back to looked at some previous examples. I got it working thanks to you. I appreciate your patience. I’m now getting the right answer when I run the console log.

 //counting cards//

var count = 0;

function cc(card) {
  /

    if (card===2 || card===3||card=== 4 || card===5 ||card=== 6) {
         count++;
      }

      else if (card===10 || card==='K' || card==='Q' || card==='J' || card==='A') {
        count--;
      }

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


  }


}




cc(2); cc(3); cc('K'); cc('K'); cc('A'); 
console.log(count);

Good job getting it working!

oops. thanks. I literally just posted another one incorrectly. I guess you are working on it now since I can’t seem to edit it.

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