无论条件如何,Javascript都会遍历所有if语句

时间:2014-12-04 11:17:32

标签: javascript

EDIT3:我现在已经完成了这项工作,但是当我尝试通过它时,我遇到了一个错误。会发生什么,它会评估每个if语句,无论它是否被正确比较。我试图避免使用字符串来使用它 - 但是当我将g,n,b移动到int时:它根本不起作用。

    <input type='button' class='button' value='Option 1' style="left: 100px" onclick = "choice(g);" />
    <input type='button' class='button' value='Option 2' style="center" onclick = "choice(n);" />
    <input type='button' class='button' value='Option 3' style="right: 100px" onclick = "choice(b);" />

我的按钮。 这是我的职责:

    var g, n, b;
    var KarmaScore = 0;  //tracks quality of your decisions
    var QuestionNumber = 1; //tracks when final question, or "Important" decisions are made.
    var end = 0; //Will be the value of your decision when you make a choice.

    function choice(move) {     
      //tracks the current question

        var playermove;
        playermove = move;

        if (playermove == g) {
        alert("Good works");
        KarmaScore++;
        end = 1;
        }
            if (playermove == n) {
            if (KarmaScore>0) {
            alert("N + works");
            KarmaScore--;
            end = 0;
                } else {
                    alert("N - works");
                    KarmaScore++;
                    end = 0;
                }
            }
        if (move == b) {
        alert("B works");
        KarmaScore--;
        end = 2;
        }
    QuestionNumber++;
    playermove = 0;
    q.src = "q"+QuestionNumber+".jpg";
    alert(KarmaScore)

} 感谢帮助。 :)

2 个答案:

答案 0 :(得分:0)

你错过了在} 之后关闭 NeutralChoice()的功能。

答案 1 :(得分:0)

您需要初始化变量KarmaScore并且您缺少关闭函数NeutralChoice()的大括号。

var KarmaScore = 0;

function GoodChoice() {
  alert("Good works");
  KarmaScore++;
  QuestionNumber++;
  Question();
  end = 1;
}

function NeutralChoice() {
  if (KarmaScore > 0) {
    alert("N + works");
    KarmaScore--;
    QuestionNumber++;
    Question();
    end = 0;
  } else {
    alert("N - works");
    KarmaScore++;
    QuestionNumber++;
    Question();
    end = 0;
  }
}

function BadChoice() {
  alert("B works");
  KarmaScore--;
  QuestionNumber++;
  Question();
  end = 2;
}
<input type='button' class='button' value='Option 1' style="left: 100px" onclick="GoodChoice();" />
<input type='button' class='button' value='Option 2' style="center" onclick="NeutralChoice();" />
<input type='button' class='button' value='Option 3' style="right: 100px" onclick="BadChoice();" />

相关问题