LEARNING JAVASCRIPT - Trang 87

const

bets

=

{

crown

:

0

,

anchor

:

0

,

heart

:

0

,

spade

:

0

,

club

:

0

,

diamond

:

0

};

let

totalBet

=

rand

(

1

,

funds

);

if

(

totalBet

===

7

) {

totalBet

=

funds

;

bets

.

heart

=

totalBet

;

}

else

{

// distribute total bet

}

funds

=

funds

-

totalBet

;

We’ll see later that the

else

part of the

if...else

statement is optional.

do…while Loop

When Thomas doesn’t pull out 7 pence by chance, he randomly distributes the funds

among the squares. He has a ritual for doing this: he holds the coins in his right hand,

and with his left hand, selects a random number of them (as few as one, and as many

as all of them), and places it on a random square (sometimes he places a bet on the

same square more than once). We can now update our flowchart to show this ran‐

dom distribution of the total bet, as shown in

Figure 4-4

.

Figure 4-4. Crown and Anchor simulation: distribute bets flowchart

A Control Flow Primer | 63