0

I'm trying to assign three random numbers to three variables (random1, random2, random3), and then assign these random variables to three elements. But I don't want any of them to be equal to the variable Sum which is the addition of two numeric innerHTML values.

So I have done that using do...while loop, but unfortunately the do...while loop doesn't work as expected .

Here is my code :

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);

Looking at the code above, it seems to be impossible for the ch2.innerHTML, ch3.innerHTML and ch4.innerHTML to be equal to Sum, but when I test it the reality says something else. Why is this?

4

2 回答 2

2

首先,正如许多人提到的那样, 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);

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

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

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);
于 2013-08-26T18:08:53.810 回答
1

关于范围的评论似乎在正确的轨道上。这是您的代码的相关部分:

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);

然后您的变量可能在适当的位置具有范围。只是预感,这可能还有其他问题。

于 2013-08-26T17:59:15.397 回答