Javascript乐透号码检查器

时间:2013-11-11 12:40:13

标签: javascript

您好我是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>");
  }

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

代码,

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

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

因此,您需要检查数组是否包含customerNumbers,例如

if(winningNumbers.indexOf(customerNumbers)!=-1)