三重条件做while循环

Triple conditioned do while loop

本文关键字:while 循环 条件 三重      更新时间:2023-09-26

我试图将三个随机数分配给三个变量(random1random2random3),然后将这些随机变量分配给三种元素。但我不希望它们中的任何一个等于变量Sum,即两个数值innerHTML的相加。

所以我已经使用do...while循环完成了这项工作,但不幸的是,do...while循环并没有按预期工作。

这是我的代码:

setTimeout(function () {
    z.innerHTML = Math.floor((Math.random() * 3) + 1);
    setTimeout(function applySUM() {
        var Sum = parseInt(document.getElementById('fir').innerHTML) +
            parseInt(document.getElementById('sec').innerHTML);
        ch1.innerHTML = Sum;
    }, 500);
    do {
        var random1 = Math.floor((Math.random() * 3) + 1);
        var random2 = Math.floor(Math.random() * (6 - 4 + 1)) + 4;
        var random3 = Math.floor(Math.random() * (10 - 7 + 1)) + 7;
    } while (random1 == Sum || random2 == Sum || random3 == Sum);
    setTimeout(function func() {
        ch2.innerHTML = random1;
    }, 1000);
    setTimeout(function func() {
        ch3.innerHTML = random2;
    }, 1500);
    setTimeout(function func() {
        ch4.innerHTML = random3;
    }, 2000);
}, 2000);

从上面的代码来看,ch2.innerHTMLch3.innerHTMLch4.innerHTML似乎不可能等于Sum,但当我测试它时,现实表明了另一点。为什么会这样?

首先,正如许多人所提到的,sum变量是ApplySum的本地变量,因此代码的其余部分引用全局sum变量(默认情况下为"未定义")

另一个问题是,现在do while循环立即运行,没有等待500毫秒的超时,并且在Sum被分配给值之前。您可以通过将代码放入settimeout回调中来解决此问题:

z.innerHTML = Math.floor((Math.random() * 3) + 1);
setTimeout(function applySUM() {
    var Sum = parseInt(document.getElementById('fir').innerHTML) +
        parseInt(document.getElementById('sec').innerHTML);
    ch1.innerHTML = Sum;

    do {
        var random1 = Math.floor((Math.random() * 3) + 1);
        var random2 = Math.floor(Math.random() * (6 - 4 + 1)) + 4;
        var random3 = Math.floor(Math.random() * (10 - 7 + 1)) + 7;
    } while (random1 == Sum || random2 == Sum || random3 == Sum);
    setTimeout(function func() {
        ch2.innerHTML = random1;
    }, 500);
    setTimeout(function func() {
        ch3.innerHTML = random2;
    }, 1000);
    setTimeout(function func() {
       ch4.innerHTML = random3;
    }, 1500);
}, 500);

(我还从其他设置中减少了500ms,以补偿它们在第一个超时内移动)

你可以考虑的另一个微小的变化是为每个变量做一个单独的循环,而不是为所有变量都做一个循环。

var random1, random2, random3;
do { random1 = Math.floor((Math.random() * 3) + 1);          } while (random1 == Sum);
do { random2 = Math.floor(Math.random() * (6 - 4 + 1)) + 4;  } while (random2 == Sum);
do { random3 = Math.floor(Math.random() * (10 - 7 + 1)) + 7; } while (random3 == Sum);

关于scope的评论似乎是正确的。以下是您代码的相关部分:

setTimeout(function applySUM() {
    var Sum = parseInt(document.getElementById('fir').innerHTML) +
        parseInt(document.getElementById('sec').innerHTML);
    ch1.innerHTML = Sum;
}, 500);
// Outside of your applySum function, Sum has no meaning
do {
    var random1 = Math.floor((Math.random() * 3) + 1);
    var random2 = Math.floor(Math.random() * (6 - 4 + 1)) + 4;
    var random3 = Math.floor(Math.random() * (10 - 7 + 1)) + 7;
} while (random1 == Sum || random2 == Sum || random3 == Sum);
// Outside of your loop body, random1, random2, and random3 have no meaning
// undefined == undefined => true

也许如果你把它改成这个:

var Sum = 0;
setTimeout(function applySUM() {
    Sum = parseInt(document.getElementById('fir').innerHTML) +
        parseInt(document.getElementById('sec').innerHTML);
    ch1.innerHTML = Sum;
}, 500);
var random1 = random2 = random3 = undefined;
do {
    random1 = Math.floor((Math.random() * 3) + 1);
    random2 = Math.floor(Math.random() * (6 - 4 + 1)) + 4;
    random3 = Math.floor(Math.random() * (10 - 7 + 1)) + 7;
} while (random1 == Sum || random2 == Sum || random3 == Sum);

然后,您的变量可能在适当的位置具有作用域。只是一种预感,这可能还有其他问题。