-6

Why doesn't it work? I am trying now for a long time and there seem to be no end to it. Need to make it simple as possible

var firstInput = document.luckForm.numberBox.value;
 var secondInput = document.luckForm.numberBox2.value;
 var thirdInput = document.luckForm.numberBox3.value;



for (firstInput = 0; firstInput <= 10; firstInput++) {
if (firstInput = luckyNumber) {
if (secondInput = luckyNumber2) {
if (thirdInput = luckyNumber3) {

{alert('Congratulations! You got all 3 numbers correct. You\'ve won £1000!'); }}}}}

I was trying to change this code (see below) into a for loop code, very simple with no arrays. I am not sure how I can do this. It is supposed to be for a lottery, you can type in three numbers and if they are correct you would get the message that you win 1000 pounds.

if (firstInput == luckyNumber && secondInput == luckyNumber2 && thirdInput == luckyNumber3 || firstInput == luckyNumber && secondInput == luckyNumber3 && thirdInput == luckyNumber2 || firstInput == luckyNumber2 && secondInput == luckyNumber3 && thirdInput == luckyNumber || firstInput == luckyNumber2 && secondInput == luckyNumber && thirdInput == luckyNumber3 || firstInput == luckyNumber3 && secondInput == luckyNumber && thirdInput == luckyNumber2 || firstInput == luckyNumber3 && secondInput == luckyNumber2 && thirdInput == luckyNumber)
    {
        alert('Congratulations! You got all 3 numbers correct. You\'ve won £1000!');
4

3 回答 3

3

您正在分配而不是比较

firstInput = luckyNumber

应该:

firstInput == luckyNumber

或者更好:

parseInt(firstInput, 10) === luckyNumber
于 2012-10-26T20:19:26.237 回答
1

这里不需要 FOR 循环。这些数字输入一次,然后与幸运数字进行核对。无需遍历循环并一遍又一遍地检查相同的数字。

如果您想循环获取数字的整个过程,然后将这些数字与幸运数字进行比较,如果它们不匹配则重复,您应该使用 DO-WHILE 循环。

做 - 获取数字并将它们与幸运数字核对。

WHILE - 数字不等于幸运数字。

于 2012-10-26T23:58:23.103 回答
0

看起来该声明只是试图确保顺序无关紧要。因此,如果您将第一个号码放在第二个盒子中,将第二个号码放在第一个盒子中,但仍然得到与乐透相同的三个号码,您仍然会赢。

(即如果结果为 15、23、35,则为 23、35、15 胜)

于 2012-10-26T23:57:55.130 回答