ga-wdi-exercises / project1

[project] GA- Project 1
3 stars 75 forks source link

issue with counting variable #196

Closed tkentor closed 9 years ago

tkentor commented 9 years ago

I tried creating a variable that recorded clicks for each question. I expected the variable to continue counting for each question, but it was out of scope. I'm not sure how I get the counter to continue through the entirety of my game.

RobertAKARobin commented 9 years ago

Can you show some code?

tkentor commented 9 years ago
  var incorrect = 0;
  var correct = 0;

    if (currentQuestion == 1) {
      $("#jack").on("click", (function(){
        $("#jack").attr("src", "farmer.png")
        incorrect ++;
        console.log(incorrect, "incorrect")
      }));
      $("#lucille").on("click", (function(){
        $("#lucille").attr("src", "farmer.png")
        incorrect ++;
        console.log(incorrect, "incorrect")
      }))
      $("#cartman").on("click", (function(){
        $("#cartman").attr("src", "farmer.png")
        incorrect ++;
        console.log(incorrect, "incorrect")
      }))
      $("#donald").on("click", (function(){
        $("#donald").attr("src", "toilet.png")
        correct ++;
        console.log(correct, "correct")
      }))
    }

    if (currentQuestion == 2) {
      $("#jack").on("click", (function(){
        $("#jack").attr("src", "farmer.png")
        incorrect += 1;
        console.log(incorrect, "incorrect")
      }));
      $("#lucille").on("click", (function(){
        $("#lucille").attr("src", "toilet.png")
        correct += 1;
        console.log(correct, "correct")
      }))
      $("#cartman").on("click", (function(){
        $("#cartman").attr("src", "farmer.png")
        incorrect += 1;
        console.log(incorrect, "incorrect")
      }))
      $("#donald").on("click", (function(){
        $("#donald").attr("src", "farmer.png")
        incorrect += 1;
        console.log(incorrect, "incorrect")
      }))
    }
jsm13 commented 9 years ago

Is your project1 repo up to date? I'm cloning it down so I can look at it as a whole

tkentor commented 9 years ago

Yeah it is, thanks

RobertAKARobin commented 9 years ago

Can you show the text of the error you were getting?

jsm13 commented 9 years ago

So I'm not seeing any correct is undefined or incorrect is undefined which is there error you'll see when a variable is out of scope. The problem I'm seeing is the issue with the event listeners on old questions sticking around so on question two for example, clicking Trump will increment correct with the event listener from question one and then increment incorrect with the event listener attached on question two.

RobertAKARobin commented 9 years ago

John's on his way!