LEARNING JAVASCRIPT - Trang 81

ture of Thomas’s behavior, we can draw a flowchart to describe it, shown in

Figure 4-1

.

Figure 4-1. Crown and Anchor simulation flowchart

In a flowchart, the diamond shapes represent “yes or no” decisions, and the rectangles

represent actions. We use circles to describe where to start and end.
The flowchart—as we’ve drawn it—isn’t quite ready to be turned directly into a pro‐

gram. The steps here are easy for a human to understand, but too sophisticated for

computers. For example, “roll dice” would not be obvious to a computer. What are

dice? How do you roll them? To solve this problem, the steps “place bets,” “roll dice,”

and “collect winnings” will have their own flowcharts (we have indicated this in the

flowchart by shading those actions). If you had a big enough piece of paper, you

could put them all together, but for the purposes of this book, we’ll present them sep‐

arately.
Also, our decision node is too vague for a computer: “broke or won 100 pence?” isn’t

something a computer can understand. So what can a computer understand? For the

purposes of this chapter, we’ll restrict our flowchart actions to the following:

• Variable assignment:

funds = 50

,

bets = {}

,

hand = []

• Random integer between m and n, inclusive:

rand(1, 6)

(this is a “helper func‐

tion” we will be providing later)

• Random face string (“heart,” “crown,” etc.):

randFace()

(another helper func‐

tion)

• Object property assignment:

bets["heart"] = 5

,

bets[randFace()] = 5

• Adding elements to an array:

hand.push(randFace())

A Control Flow Primer | 57

Liên Kết Chia Sẽ

** Đây là liên kết chia sẻ bới cộng đồng người dùng, chúng tôi không chịu trách nhiệm gì về nội dung của các thông tin này. Nếu có liên kết nào không phù hợp xin hãy báo cho admin.