-3

EDIT3: I have this mostly completed now, but I'm running into an error when I try to go through it. What happens is that it evaluates every if statement, regardless of whether or not it is compared correctly. I've tried to avoid using a string to use this - but when I sub move for g,n,b to an int: it doesn't work at all.

    <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);" />

There's my buttons. Here's my functions:

    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)

} Help is appreciated. :)

Garrett C.
  • 23
  • 1
  • 6
  • 3
    Look in the browser's error console to see the error messages. And indent your code so you'll see the mismatching brackets more easily. – JJJ Dec 04 '14 at 11:21
  • I feel pretty dumb, it says my functions aren't defined - but I cannot figure out what or where to declare it. – Garrett C. Dec 04 '14 at 12:53
  • The fiddle says "invalid left-hand side in assignment", nothing about functions. You need to make a difference between `=` (assignment) and `==` (comparison). Also see http://stackoverflow.com/questions/5431351/simple-example-doesnt-work-on-jsfiddle – JJJ Dec 04 '14 at 12:55
  • In that example, how does the javascript associate that snippet of code with the HTML? – Garrett C. Dec 04 '14 at 13:45
  • Thanks, you helped out the most! I didn't know I could error search with the browser. – Garrett C. Dec 04 '14 at 17:54

2 Answers2

0

You miss close the NeutralChoice() function with } after the else.

cesar_sr91
  • 130
  • 9
0

You need to initialize the variable KarmaScore and you were missing closing curly brackets for the function 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();" />
Weafs.py
  • 22,731
  • 9
  • 56
  • 78