太短的循环和无限循环?

时间:2015-08-24 14:19:17

标签: javascript loops for-loop

我正在尝试制作代表数学中大数定律的代码。但是,我一直在尝试通过一次执行代码来打印20个不同的数字。如果我将 for loop 限制为很少(即任何小于12的数字),它将只打印出一个数字。如果我的限制太大(数字大于13),它将打印出一组无数的数字。

var numPeople = [];
var coin = [0, 1];
var sameSide = [];
for (var i = 0; i < 9; i++) {
    numPeople.push(i);
}

function coinFlip() {
    for (var i = 0; i < numPeople.length; i++) {
        coinChance = Math.floor(Math.random() * 100);
        if (coinChance < 50) {
            coinSide = coin[1];
        } else {
            coinSide = coin[2];
        }
        numPeople[i] = coinSide;
    }
}

for (var i = 0; i < 2; i++) {
    coinFlip();
    for (var i = 0; i < numPeople.length; i++) {
        for (var j = i; j < numPeople.length - i; i++) {
            if (numPeople[i] === numPeople[j]) {
                sameSide.push(true);
            }
        }
    }
    console.log(sameSide.length);
}

0 个答案:

没有答案