This is the freeCodeCamp Counting Cards activity. https://www.freecodecamp.org/learn/javascript-algorithms-and-data-structures/basic-javascript/counting-cards
My solution was
let count = 0;
function cc(card) {
// Only change code below this line
if (card = 2 || 3 || 4 || 5 || 6) {
count + 1;
} else if (card = 7 || 8 || 9) {
return;
} else if (card = 10, "J", "Q", "K", "A")
count - 1;
}
if (count > 0) {
return count + " " + "Bet";
} else if {
return count + " " + "Hold";
}
// Only change code above this line
}
cc(2); cc(3); cc(7); cc('K'); cc('A');
I checked the solution and it seemed similar. Why didn't it work?
A Few Modifications were necessary for this code to work.
= is assignment operator while == is comparing operator.else if can be replaced with just else if there is no condition.else if (card = 10, "J", "Q", "K", "A") didnt have closing or opening brackets.return at else if (card == 7 || 8 || 9) { and instead use count += 0|| Or operator like this card == 7 || card == 8 and not like card == 7 || 8count++ and count-- instead of count + 1 and count - 1
After all these changes the below code works fine.let count = 0;
function cc(card) {
// Only change code below this line
if (card == 2 || card == 3 || card == 4 || card == 5 || card == 6) {
count++;
} else if (card == 7 || card == 8 || card == 9) {
count += 0;
} else if (card == 10|| card == "J"|| card == "Q"|| card == "K"|| card == "A"){
count--;
}
if (count > 0) {
return count + " " + "Bet";
} else {
return count + " " + "Hold";
}
}
cc(2); cc(3); cc(7); cc('K'); cc('A');
= is used to assign a value and not to make comparison. In javascript we use == or === to make comparison, and also count - 1 will not update count. use count = count - 1 or count++
let count = 0;
function cc(card) {
// Only change code below this line
if ( card >= 2 && card <= 6) {
count = count + 1;
} else if (card >= 7 && card <= 9) {
return;
} else if (card === 10 || card === "J" || card === "Q" || card === "K" || card === "A")
count = count - 1;
}
if (count > 0) {
return count + " " + "Bet";
} else if {
return count + " " + "Hold";
}
// Only change code above this line
}
cc(2); cc(3); cc(7); cc('K'); cc('A');
There are two main issues with your code.
if conditionYour if condition card = 2 || 3 || 4 || 5 || 6 does not work like you think it does. You would need to use car === 2 || card === 3 || car === 4 || card === 5 || car === 6. You cannot use the assignment operator = like this. You should use the strict comparison operator instead.
It's even easier to use an array and includes() like this [2, 3, 4, 5, 6].includes(card).
count is not updatedYou need to assign the new value to count when you increment/ decrement it. You can use count++ instead of count + 1 which is the equivalent to count = count + 1.
let count = 0;
function cc(card) {
// Only change code below this line
if ([2, 3, 4, 5, 6].includes(card)) {
count++;
} else if ([10, "J", "Q", "K", "A"].includes(card)) {
count--;
}
if (count > 0) return `${count} Bet`;
return `${count} Hold`;
}
cc(2); cc(3); cc(7); cc('K'); cc('A');