HTML and CSS Reference
In-Depth Information
JavaScript (and other programming languages) distinguish between numbers and strings of characters
representing numbers. That is, the value "100" is a string of characters, "1","0", and "0". The value 100 is a
number. In either case, however, the value of a variable is stored as a sequence of 1s and 0s. For
numbers, this will be the number represented as a binary number. For strings of characters, each
character will be represented using a standard coding system, such as ASCII or UNICODE. In some
situations, JavaScript will make the conversion from one data type to the other, but don't depend on it.
The coding I suggest uses the built-in functions String and Number to do these conversions.
In the throwdice function, before the if(firstturn) statement, add the code in Table 2-6 (or something
like it).
Table 2-6. Adding a Bank for the Player
Code
Explanation
var bank = Number(document.f.bank.value); Sets a new variable bank to be the number
represented by the value in the bank input field.
if (bank<10) {
Compare bank to 10.
alert("You ran out of money! Add
some more and try again.");
If bank is less than 10, put out an alert.
Return;
Exit the function without doing anything.
}
Close the if true clause.
bank = bank - 10;
Decrease bank by 10. This line is reached only when
bank was greater than 10.
document.f.bank.value = String(bank);
Put the string representation of that value in the
bank field.
Then in each place where the player wins (in the switch statement for a first turn after the 7 and 11 cases,
or in the switch statement for a follow-up turn, after the point case, add the code in Table 2-7.
Table 2-7. Increasing the Value of the Bank
Code
Explanation
bank = Number(document.f.bank.value);
Set bank to be the number represented by the value in
the bank input field. Setting bank again allows for the
possibility of the player re-setting the bank amount in
the middle of a game.
Search WWH ::




Custom Search