if…else Statement
One of the shaded boxes in our flowchart is “place bets,” which we’ll fill out now. So
how does Thomas place bets? Thomas has a ritual, as it turns out. He reaches into his
right pocket and randomly pulls out a handful of coins (as few as one, or as many as
all of them). That will be his funds for this round. Thomas is superstitious, however,
and believes the number 7 is lucky. So if he happens to pull out 7 pence, he reaches
back into his pocket and bets all his money on the “Heart” square. Otherwise, he ran‐
domly places the bet on some number of squares (which, again, we’ll save for later).
Let’s look at the “place bets” flowchart in
Figure 4-3. Crown and Anchor simulation: place bets flowchart
The decision node in the middle (
totalBet === 7
) here represents an
if...else
statement. Note that, unlike the
while
statement, it doesn’t loop back on itself: the
decision is made, and then you move on. We translate this flowchart into JavaScript:
62 | Chapter 4: Control Flow