【问题标题】:Javascript logical operatorsJavascript 逻辑运算符
【发布时间】:2013-11-04 18:52:51
【问题描述】:

我正在尝试编写一个彩票游戏,其中在 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;
 }

有人能指出正确的方向吗?

【问题讨论】:

  • &lt;font&gt; 标签? &lt;center&gt; 标签?这是什么,1996 年??
  • 你能问一个具体的问题吗? “不工作”不是一个。
  • @gdoron - 我们缺少的只是眨眼和选框。
  • 您的onclick 调用了不存在的myCheck()
  • "Better luck next time. Your numbers are "+a+" , "+b+" , "+c""

标签: javascript integer


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

    检查此套装以了解您的应用程序逻辑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; 
    
    
    }//]]>   `
    

    【讨论】:

      猜你喜欢
      • 2011-09-23
      • 1970-01-01
      • 1970-01-01
      • 2013-06-01
      • 2020-10-02
      • 1970-01-01
      • 1970-01-01
      • 2017-05-03
      • 1970-01-01
      相关资源
      最近更新 更多