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);
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
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.
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.