Figure 4-6. Crown and Anchor simulation: collect winnings flowchart
Putting It All Together
We would need a large piece of paper to put all of the pieces of the flowchart together,
but we can write the whole program out fairly easily.
In the following program listing (which includes the helper functions), there are also
some calls to
console.log
so you can observe Thomas’s progress (don’t worry about
understanding how the logging works: it’s using some advanced techniques we’ll
learn about in later chapters).
We also add a
round
variable to count the number of rounds Thomas plays, for dis‐
play purposes only:
// returns a random integer in the range [m, n] (inclusive)
function
rand
(
m
,
n
) {
return
m
+
Math
.
floor
((
n
-
m
+
1
)
*
Math
.
random
());
}
66 | Chapter 4: Control Flow