-3

我正在尝试编写一个彩票游戏,其中在 3 个单独的文本框中输入的数字 3、5、8 将产生一个成功警报框,否则一个失败警报框也会显示输入的错误数字。

我已将代码放在这里,但我不知道为什么它不起作用!

演示:http: //jsfiddle.net/9MsCY/4/

//correct numbers
var a1 = 3;
var a2 = 5;
var a3 = 8;

function mainGame(num1, num2, num3) {

   var num1 = document.getElementById("a").value;
   var num2 = document.getElementById("b").value;
   var num3 = document.getElementById("c").value;

   var a = num1;
   var b = num2;
   var c = num3;

   if (isUnique(a,b,c)) {

       if (checkGuess(a,b,c)) {
            alert("Congratulations! You got all 3 numbers correct, you've won £1000!");
       } else {
            alert("Better luck next time. Your numbers are "+a+" , "+b+" , "+c"");
       }
    }
 }

//function in while loop to check for unique numbers and for not null
function isUnique() {

    var len = arguments.length;
    for (var i = 1; i < len; i++) {
        if (arguments[i] == null || arguments[i] = arguments[i-1]) {
            return false;
        }
    }
    return true;
}

//compare inputs with correct numbers
function checkGuess () {
    if (a == 3 && b == 5 && c == 8) {
       return true;
    }
    return false;
 }

有人可以指出我正确的方向吗?

4

2 回答 2

0

如上所述,您的代码有语法错误。我已将其删除并开始工作。

检查这件衣服是否适合您的应用程序逻辑 http://jsfiddle.net/btYpQ/

`/function in while loop to check for unique numbers and for not null
function isUnique() {
var len = arguments.length;
for (var i = 1; i < len; i++) {
   if (arguments[i] == null || arguments[i] == arguments[i-1]) {
    return false;
   }
 }
        return true;
}


//compare inputs with correct numbers
function checkGuess () {

if (a == 3 && b == 5 && c == 8) {
   return true;
   }
   return false;

}
function mainGame() {

var num1 = document.getElementById("a").value;
var num2 = document.getElementById("b").value;
var num3 = document.getElementById("c").value;

var a = num1;
var b = num2;
var c = num3;


if (isUnique(a,b,c)) {

    if (checkGuess(a,b,c)) {
        alert("Congratulations! You got all 3 numbers correct, you've won £1000!");
 }   
        else {
        alert("Better luck next time. Your numbers are "+a+" , "+b+" , "+c);
        }
}



}

window.onload=function(){
//correct numbers

var a1 = 3;
var a2 = 5;
var a3 = 8; 


}//]]>   `
于 2013-11-04T19:29:22.667 回答
0

您的代码有很多语法错误。调试 javascript 代码的最佳方法是编写 html + js,在 chrome 浏览器中浏览您的页面并使用 chrome 开发人员工具。它会为您指明正确的方向。

于 2013-11-04T19:08:08.620 回答