Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.

# Thread: Does it matter what value we give the bet?

1. ## Does it matter what value we give the bet?

Code:
```function rollthedice() //

{

money=100;

bet=1000;
alert ("Play til you lose it all");

while (money>=1)

{

//no betting more money than you have!

//the line below will go forever...the if...break will

//get you out of this infinite betting loop if you bet legally

while (1==1)

{

pick=window.prompt("Please pick over 14(o) or under 14(u)","u");

if (bet<=money && bet>0) break; //

}

die1=Math.floor(Math.random()*6+1);
die2=Math.floor(Math.random()*6+1);

die3=Math.floor(Math.random()*6+1);

die4=Math.floor(Math.random()*6+1);

//the *6 gives you 6 numbers and the 1 says start at 1 so

//we randomly get numbers between 1 and 6 like on a die

total=die1+die2+die3+die4;

document.write("</br>You rolled "+die1+" and "+die2+" and "+die3+" and "+die4+" total = "+total);

document.write("</br>You picked "+pick+" and rolled "+total);

//you win if you pick u and you get less than 14

//OR you pick o and you have more than 14

if ((pick=="u" && total<14) || (pick=="o" && total>14))

{

money=money+bet;

document.write(" You won! You now have \$"+money);

}

else if (total==14)

{

document.write(" 14 is a tie...no winner..keep your bet");

}

else

{

money=money-bet;
document.write(" You Lost! You now have \$"+money);

} //end of the if statement

} //end of the---lets play WHILE we have more than \$1 left

} //end of the function

</script>```

• No. It should really read var bet; (Note the use of the var keyword which you have omitted throughout).

Prompt is a very primitive way to obtain user input. Alerts should be used only for debugging.

As has been explained a zillion times in this forum, document.write() is in effect obsolete. document.write() statements must be run before the page finishes loading. Any document.write() statement that runs after the page finishes loading will create a new page and overwrite all of the content of the current page (including the Javascript which called it). So document.write() is at best really only useful to write the original content of your page. It cannot be used to update the content of your page after that page has loaded.

In short, you are being taught to write antiquated and long-obsolete code! There are many other problems with your code, particularly the absence of any input verification.

All advice is supplied packaged by intellectual weight, and not by volume. Contents may settle slightly in transit.

• The great advantage of being on iBall's ignore list is that, although many of his posts simply repeat what has already been said, he cannot seize on something in one of my posts to start a pointless and belligerent argument.

•

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•