JavaScript 控制流全解析
1. Crown and Anchor 模拟程序
在 Crown and Anchor 模拟程序中,我们可以将流程图的各个部分整合起来编写完整的程序。以下是完整的代码实现:
// returns a random integer in the range [m, n] (inclusive)
function rand(m, n) {
return m + Math.floor((n - m + 1)*Math.random());
}
// randomly returns a string representing one of the six
// Crown and Anchor faces
function randFace() {
return ["crown", "anchor", "heart", "spade", "club", "diamond"]
[rand(0, 5)];
}
let funds = 50; // starting conditions
let round = 0;
while(funds > 1 && funds < 100) {
round++;
console.log(`round ${round}:`);
console.log(`\tstarting funds: ${funds}p`);
// place bets
let bets = { crown: 0, anchor: 0, heart: 0,
spade:
超级会员免费看
订阅专栏 解锁全文

被折叠的 条评论
为什么被折叠?



