-1

我现在正在尝试一段时间并让它变得完美。我正在尝试简化我创建的这个for 循环并使其实际工作,没有任何数组,只有最基本的基本 JavaScript。

for (var x=0;x<=1;x++) { 
    if (secondInput == luckyNumber || secondInput == luckyNumber2 || secondInput == luckyNumber3) { 
        if (thirdInput == luckyNumber || thirdInput == luckyNumber2 || thirdInput == luckyNumber3) {
            if (firstInput == luckyNumber || firstInput == luckyNumber2 || firstInput == luckyNumber3) {
                while (firstInput !== secondInput){
                    while(firstInput !== thirdInput){while(secondInput !== thirdInput) {
                        alert('Congratulations! You got all 3 numbers correct. You\'ve won £1,000!');
                    }
                }
            }
        }
    }
}

这段代码有意义还是我做错了什么?我有一种感觉,我什至可以离开循环,但这是我认为它是正确的唯一方法。

4

3 回答 3

1

编写一个函数,接受输入,将其与幸运数字进行比较,并返回一个布尔值和结果。

if在您的子句中调用该函数。

我不太明白你想用while循环做什么。

于 2012-10-30T20:30:22.317 回答
1

您可以尝试使用这个想法来帮助:

[1, 3, 2].sort()

(将您的问题和答案存储在数组中,然后对两者进行排序然后比较。当然,检查 javascript 数组是否相等是一个有趣的新项目 :))

于 2012-10-30T20:39:13.957 回答
0

干得好。你说你希望它简化。

for (var x = 0; 1 >= x; x++) {
if (!(secondInput != luckyNumber && secondInput != luckyNumber2 && secondInput != luckyNumber3 || thirdInput != luckyNumber && thirdInput != luckyNumber2 && thirdInput != luckyNumber3 || firstInput != luckyNumber && firstInput != luckyNumber2 && firstInput != luckyNumber3)) {
    while (firstInput !== secondInput) {
        while (firstInput !== thirdInput) {
            while (secondInput !== thirdInput) {
                alert("Congratulations! You got all 3 numbers correct. You\'ve won £1,000!");
            }
        }
    }
}
}
于 2012-11-09T00:46:35.813 回答