<PLEASE USE THE FOLLOWING TEMPLATE TO HELP YOU CREATE A GREAT POST!>
<Below this line, add a link to the EXACT exercise that you are stuck at.>
<In what way does your code behave incorrectly? Include ALL error messages.>
Code runs up until the if statement in the selectButton function. I’ve tested to make sure the function has access to the necessary variables, so why won’t the if statement run?
```
var selectButton = function(){
button = prompt(“Enter the number of the player who will deal the first hand:”, “1”);
document.write("
" + playerNum + “players.”);
document.write("
" + "Button is player " + button + “.”);
if(button > playerNum) {
document.write(“You only have " + playerNum + " players. Enter the number of the player who will deal the first hand:”);
}
}
var setStackSizes = function(){
playerStacks = ;
document.write("
");
for(i=1; i<=playerNum;){
playerStacks[i] = prompt(“Player " + i + " enter your buy-in:”);
if(playerStacks[i]>=(2*BB)){
i++;
}
}
playerStacks.toString();
document.write(playerStacks);
selectButton();
}
var setBlinds = function(){
BB = prompt(“Enter Big Blind:”);
var SB = BB/2;
document.write(“
Small Blind/Big Blind is $” + SB + “/” + BB);
setStackSizes();
}
var startGameErr = function() {
playerNum = prompt(“Games can have 2-9 players. Enter number of players:”, “”);
if(playerNum <= 9 && playerNum >= 2) {
document.write(playerNum + " players");
setBlinds();
}
else {
startGameErr();
}
}
var startGame = function(){
playerNum = prompt(“Enter number of players:”);
if(playerNum <= 9 && playerNum >= 2) {
document.write(playerNum + " players");
setBlinds();
}
else {
startGameErr();
}
}
startGame();
<do not remove the three backticks above>
Hi
I tried your code but it seems that the for loop in the function setStackSizes is not working correctly. Several comments :
-
I would not use a standard for loop if the i is incremented inside the loop. I would use a do/while loop where you can increment i inside the loop.
-
Don’t forget that arrays begin at index 0 in JS, not at index 1
-
you have to declare i as a local variable (var i = 0;
you forgot the var).
-
I noticed that the i is not incremented even if i < playerNum and playerStacks[i]>=(2*BB)… there is something strange happening
-
Don’t forget the semi-colons at the end of the functions :
var setStackSizes = function(){
...
} ; # <------ semi-colon here and for all the other functions
Cheerio
1 Like
thx. I gotta run, but i’ll make these edits in a bit.
I’m not sure why the i wasn’t incrementing when you ran it, it worked for me. That said, I switched it to your way and it works all the same. I still have the same problem though. The if statement in selectButton() won’t work, but the variables involved both print out, so I can’t figure out why. I’m using notepad++ and running in Google Chrome. Here’s the new code:
var selectButton = function(){
button = prompt(“Enter the number of the player who will deal the first hand:”, “1”);
document.write("
" + playerNum + “players.”);
document.write("
" + "Button is player " + button + “.”);
if(button > playerNum) {
document.write(“You only have " + playerNum + " players. Enter the number of the player who will deal the first hand:”);
}
};
var setStackSizes = function(){
playerStacks = ;
document.write("
");
var i = 0;
do {
playerStacks[i] = prompt(“Player " + (i+1) + " enter your buy-in:”);
if(playerStacks[i]>=(2*BB)){
i++;
}
}while(i<playerNum);
playerStacks.toString();
document.write(playerStacks);
selectButton();
};
var setBlinds = function(){
BB = prompt(“Enter Big Blind:”);
var SB = BB/2;
document.write(“
Small Blind/Big Blind is $” + SB + “/” + BB);
setStackSizes();
};
var startGameErr = function() {
playerNum = prompt(“Games can have 2-9 players. Enter number of players:”, “”);
if(playerNum <= 9 && playerNum >= 2) {
document.write(playerNum + " players");
setBlinds();
}
else {
startGameErr();
}
};
var startGame = function(){
playerNum = prompt(“Enter number of players:”);
if(playerNum <= 9 && playerNum >= 2) {
document.write(playerNum + " players");
setBlinds();
}
else {
startGameErr();
}
};
startGame();
Oups I am sorry, I used an external website that simulates javascript but apparently it does not manage very well the loops. I used your code in a local html window and it does work for me. I use Firefox. I modified slightly your functions selectButton and startGame so that you can ask again for a new value for button/playerNum (the function startGameErr is not needed anymore):
var selectButton = function(){
var msg = "Enter the number of the player who will deal the first hand:";
do {
button = prompt(msg, "1");
document.write("<br/>" + playerNum + "players.");
document.write("<br/>" + "Button is player " + button + ".");
if(button > playerNum) {
msg = "You only have " + playerNum + " players. Enter the number of the player who will deal the first hand:";
document.write(msg);
}
} while (button > playerNum);
};
var setStackSizes = function(){
playerStacks = [];
document.write("<br/>");
var i = 0;
do {
playerStacks[i] = prompt("Player "+(i+1) + " enter your buy-in:");
if(playerStacks[i]>=(2*BB)){
i++;
}
} while(i<playerNum);
playerStacks.toString();
document.write(playerStacks);
selectButton();
};
var setBlinds = function(){
BB = prompt("Enter Big Blind:");
var SB = BB/2;
document.write("<br/>Small Blind/Big Blind is $" + SB + "/" + BB);
setStackSizes();
};
var startGame = function(){
var msg = "Enter number of players:";
do {
playerNum = prompt(msg, "");
if (playerNum > 9 || playerNum < 2) {
msg = "Games can have 2-9 players. Enter number of players:";
document.write("Games can have 2-9 players. You entered "+playerNum+" players.<br/>");
} ;
} while (playerNum > 9 || playerNum < 2);
document.write(playerNum + " players");
setBlinds();
};
startGame();
When I execute this code, I get
Games can have 2-9 players. You entered 10 players.
Games can have 2-9 players. You entered 20 players.
Games can have 2-9 players. You entered 26 players.
2 players
Small Blind/Big Blind is $5/10
100,200
2players.
Button is player 3.You only have 2 players. Enter the number of the player who will deal the first hand:
2players.
Button is player 4.You only have 2 players. Enter the number of the player who will deal the first hand:
2players.
Button is player 5.You only have 2 players. Enter the number of the player who will deal the first hand:
2players.
Button is player 1.
You can see that I entered in the if
in the function selectButton
. I would recommend you to change your web browser. I hope it will help
Cheerio
1 Like
Ok, thanks a lot. i’ll test this out more in depth later as well as switch to Firefox.
Cheers,
Chris
Hi Belgian,
I tested it with 2 players and got the same results as you when I entered the button as players 3-9, but when I entered 10 or more, it didn’t cycle through the if statement again like I expected and instead printed out button is player 10. I tested it with other numbers of players and for whatever reason, if you enter in 10 or more, the if statement does not work and it prints out button is player (10+).
For example:
7 players.
Small Blind/Big Blind is $0.5/1
Button is player 8. You only have 7 players.
Button is player 9. You only have 7 players.
Button is player 10.
Hi
I now know where the problem comes from. The function prompt
returns a string! Thus the variables button
and playerNum
are strings and their comparison does not work in the same way as for integers. You can convert them directly by the function parseInt
or parseFloat
(for BB and playerStacks):
button = parseInt(prompt(msg, "1"));
...
playerStacks[i] = parseFloat(prompt("Player "+(i+1) + " enter your buy-in:"));
...
BB = parseFloat(prompt("Enter Big Blind:"));
...
playerNum = parseInt(prompt(msg));
Moreover, in the function selectButton
, I would also check that button is not negative or equal to zero, so :
do {
...
} while (button > playerNum || button <=0);
Finally, are you sure that the line playerStacks.toString();
is necessary in the function setStackSizes
? Because it will force you to convert again to floats afterwards.
Now it is working perfectly on my side. I hope that it will help you 
Cheerio
1 Like
No, the toString(); it’s not necessary. I was just printing things out as I go to make sure I have access to all the variables and don’t all of the sudden lose access when a later function needs to access them.
Could you break down why playerNum gets parseInt while playerStacks gets parseFloat? I’ve never worked with those methods …
Concerning parseInt
and parseFloat
, they convert strings to integers and floats respectively. An integer is a number without decimals, such as
2 or 10 or 101
and a float is a number with decimals such as 101,25 or 0,78
I was anticipating the fact that for your game, BB
and playerStacks
contains values which might be floats because they are linked with money (and so the parseFloat
)
However, you can not have float values for the number of players and that’s why the parseInt
is used for them.
1 Like
Ah, I understand. Thanks Belgian 
For some reason when I was learning JavaScript I thought I needed toString in order to print out the array in that way, but it prints out just fine without it. Idk why I thought that.