In the .body
click handler function I am trying to take in the value of the clicked element and check it against the question's correctAnswer
.
I think the problem is that I am not correctly getting the value of the clicked element.
I tried to do it with this line: var playerGUess = $(this).val()
but i think i am missing something in .val()
.
// Create global variables for correct answers, incorrect guesses
var correctGuesses = 0;
var incorrectGuesses = 0;
var timeLeft = 30;
var questionCounter = 0;
var gameWin = false;
// Create an object containing: All questions, correct answers and 3 incorrect answers for every question. Add an interesting fact about the answer
// (3 false and 1 true)
// questions, answers, wrong guesses, true or false
var quizItems = [{
question: "What swimming stroke is named after an insect?",
correctAnswer: "Butterfly",
incorrect1: "Bee",
incorrect2: "Cricket",
incorrect3: "Wasp"
},
{
question: "The largest member of the salmon family lives in the Pacific Ocean, weighs up to 120 pounds, and is known as what?",
correctAnswer: "White Sturgeon",
answers: ["Tuna", "Cod", "Bass", "White Sturgeon"],
},
{
question: "According to the San Francisco Department of Animal Care and Control, what is the most commonly reported wild animal sighted by people in the streets of San Francisco?",
correctAnswer: "Raccoon",
incorrect1: "Rat",
incorrect2: "Possum",
incorrect3: "Fox"
},
{
question: "What substance can you drop onto a scorpions head to make it sting itself to death?",
correctAnswer: "Alcohol",
incorrect1: "Cider",
incorrect2: "Vinegar",
incorrect3: "Bleach"
},
{
question: "Cows have sweat glands in what part of their bodies? ",
correctAnswer: "Nose",
incorrect1: "Tongue",
incorrect2: "Ears",
incorrect3: "Feet"
}
];
console.log("Question: " + quizItems[questionCounter].question);
console.log("Answer: " + quizItems[questionCounter].correctAnswer);
$("#start-button").on("click", function() {
// Timer
setInterval(countdown, 1000);
$("#timer").text(timeLeft);
// Display Questions
$("#question-area").text(quizItems[questionCounter].question);
$("#answer1").text(quizItems[questionCounter].incorrect1);
$("#answer2").text(quizItems[questionCounter].incorrect2);
$("#answer3").text(quizItems[questionCounter].incorrect3);
$("#answer4").text(quizItems[questionCounter].correctAnswer);
// add event listener to all of the answers
// Whenever any answer is clicked, do questionCounter++ so the next answers can be displayed
// Same for timer
});
//Click events for all the guesses
$('body').on('click', '.answer', function() {
var playerGUess = $(this).val();
//Check the value of the button pressed to see if its right or wrong
if (playerGUess === quizItems[questionCounter].correctAnswer) {
console.log("Correct");
} else {
console.log("incorrect");
}
});
// Countdown Timer Function
function countdown() {
if (timeLeft === 0) {
} else {
timeLeft--;
$("#timer").text(timeLeft);
console.log(timeLeft);
}
};
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<div class="jumbotron" id="gameContainer">
<a class="btn btn-primary btn-lg" href="#" role="button" id="start-button">Start Game</a>
<div id="mainGame">
<div id="timer"></div>
<div id="question-area"></div>
<div id="answers-area">
<div id="answer1" class="answer"></div>
<div id="answer2" class="answer"></div>
<div id="answer3" class="answer"></div>
<div id="answer4" class="answer"></div>
</div>
</div>
</div>