-2

嗨,我是 Javascript 新手,正在尝试做一个数字检查程序。

客户编号为 12 且在中奖号码范围内。

这似乎工作并打印它是一个中奖号码,但如果我将值更改为 13,它仍然会打印中奖号码。

就像 document.write 正在打印,不管它是否在 if 语句中。

任何帮助或指导将不胜感激

谢谢

这是我的代码

 var customerNumbers = 12;

    //Array stating the winning numbers

    var winningNumbers = [12, 17, 24, 37, 38, 43];



    document.write("<h1>This Weeks Winning Numbers are:</h1>");

    //For statement to list ALL values in the winning numbers array 

    for (var i=0;i<winningNumbers.length;i++)

    {

    document.write(winningNumbers[i] + "&#44; &#32;");

    }


    document.write("<h1>The Customer's Number is:</h1>");

    document.write(customerNumbers);



    //if statement to check if customer number matches with winning numbers

    if (customerNumbers == 12 || 17 || 24 || 37 || 38 || 43)

    {
  document.write("<h3>We have a match and a winner!</h3>");
  }



    else if (customerNumbers !== 12 || 17 || 24 || 37 || 38 || 43)
  {
  document.write("<h4>Sorry you are not a winner this week</h4>");
  }


    else (customerNumbers !== 12 || 17 || 24 || 37 || 38 || 43)
  {
  document.write("<h4>Sorry you are not a winner this week</h4>");
  }
4

2 回答 2

3

您的错误在 If 和 Else 语句中

customerNumbers == 12 || 17 || 24 || 37 || 38 || 43

以上不符合您的预期,将其替换为

customerNumbers == 12 ||  customerNumbers == 17 ||  customerNumbers == 24 ||   customerNumbers == 37 ||  customerNumbers == 38 ||  customerNumbers == 43

更干净:

winningNumbers.indexOf(customerNumbers) > -1
于 2013-11-11T12:46:03.183 回答
0

编码,

customerNumbers == 12 || 17 || 24 || 37 || 38 || 43

将检查 customerNumbers 是否等于 12,否则将分配给它 17 并将 if 语句作为 true 传递。

因此,您需要通过其他方式检查数组是否包含 customerNumbers,

if(winningNumbers.indexOf(customerNumbers)!=-1)
于 2013-11-11T12:46:42.027 回答