ga-wdi-exercises / project1

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

Selecting the correct button #229

Closed tvcrawley closed 8 years ago

tvcrawley commented 8 years ago

I'm trying to increment var yes when the "correct" button is chosen.

I have it so that it increments when one of the three possible buttons is clicked but I can't seem to get the conditional right for the if statement so yes goes up only when the correct button is pushed.

I tried using an if statement but have struck out on how to compare the clicked value to the correct value (rightAnswer).

I expected the counter to increment when I press correct.

The counter goes up when any of the three buttons are clicked.

My repo link is https://github.com/Chibiania/game-lab/blob/master/script.js and my question is about lines 85 which goes to 153-161.

RobertAKARobin commented 8 years ago

One way would be to use custom attributes. I talked about that in another issue:

https://github.com/ga-wdi-exercises/project1/issues/215

That is: you could store whether an answer is "right" or "wrong" in the HTML itself.

tvcrawley commented 8 years ago

I assigned the data attribute to the correct and incorrect answers.

I tried an if/else statement to compare the dataset to its value (either correct or incorrect).

When the correct answer is chosen, I expected the counter to increment and display in the console. When an incorrect answer is chosen, I expected the wrong answer string to print to the console.

What actually happens is the counter goes up now matter which answer is chosen.

Repo link: https://github.com/Chibiania/game-lab/blob/master/script.js Lines: setting data attribute: 92, 99, 107; if/else: 156-166

RobertAKARobin commented 8 years ago

I suspect this.querySelector(".correct") is returning the wrong button. .querySelector is searching the entire $(buttonSection) for anything with the class of correct, and it's returning whatever happens to be the first one. How can you make sure it specifically gets the button you want?

tvcrawley commented 8 years ago

I've got it thanks to the geniuses!

RobertAKARobin commented 8 years ago

Yesss