# Stumped! - Stuck on JavaScript Blackjack function [SOLVED]

Stumped! - Stuck on JavaScript Blackjack function [SOLVED]
0

#1

Alright folks, currently in the Ask step of Read-Search-Ask. I’m creating a Blackjack function for the front-end JavaScript exercises using if/else if statements and I need guidance. I did see that the “Hint” page has a solution, but I’m determined to ask around and try a little harder to figure out the solutions myself, and figure out why my code is doing what it’s doing.

``````var count = 0;

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

cc(2); cc(2); cc(10);
``````

What is currently happening, is that each argument passed adds 1 to the variable count, if we’re using zero-based indexing. I think something is wrong with my if statements, but I can’t tell. I’d love a different outlook for this one!

Thanks,

Julianna

#2

Change this to if (card > 1 && card < 7 && count > 0)
Do the same for the other if conditions. You have to specify what is being compared in each “or”. For example if I wanted to test if card was 1 or 2 it would look like

``````if(card === 1 || card === 2) {
//...do stuff
}
``````

#3

A little hint for you:

``````  if (card === 2 || 3 || 4 || 5 || 6 && count > 0) {
``````

The above if statement is probably not what you meant to do. You probably meant:

``````  if ((card === 2 || card === 3 || card === 4 || card === 5 || card === 2) && count > 0) {
``````

In general your if then structure is overly complicated. It would be better to not try to combine both the counting and the text out put (Bet or Hold) logic in the same if statement. Try keeping the two separate to simplify the over all logic. When ever you notice identical code in more than one section, you can simplify the code.

#4

I’ve been staring at my if statements and thought something was wrong! Makes much more sense, thanks! I think now my trouble lies in adding the increment or decrement appropriately, but at least the 7 through 9 arguments are passing correctly.

#5

Thanks! I’m trying to simplify the “if” statement right now for values 10, “J”, “Q”, “K” and “A”. I’m also trying to figure out a way to use increments or decrements on count before returning any values, but I’m not sure if that’s possible.

#6

If you get really stuck, I created a shell structure below (hidden - but you can click on the blurred section to reveal the structure). The code below is not the completed solution, but just a guide to the if then logic. Only click on the blurred section if you are completely stuck.

``````function cc(card) {
if (card >= 2 && card <= 6) {
// write some code here
}
else if (card === 10 || (card >= "A" && card <= "Q")) {
// write some code here
}
if (count > 0) {
// write some code here
}
else {
// write some code here
}
}
``````

You only need to write code in the sections I specified. Anything else is overly complicated. Once you get a working solution with this code, there is a way to simplify the last if then else statement that I will show you.

#7

#8

Thanks every for all of your input and explanations. Stepping away from this problem for a few hours, reading everyone’s code and reviewing the if/else basics helped immensely with figuring out what was wrong with my original function. I’m going to try to find some similar problems I can practice until i can really get the swing of these statements. Here is my final code:

``````var count = 0;

function cc(card) {
var result = "";
if (card >= 2 && card <= 6) {
count++;
} else if (card === 10 || card === "J" || card === "Q" || card === "K" || card === "A") {
count--;
}
if (count <= 0) {
result = count + " Hold";
} else {
result = count + " Bet";
}
return result;
}``````