I am currently trying to build a user score function for a javascript quiz and I can’t seem to figure out how to 1) pull the answer selected based on a click event and 2) determine whether that scrore is true or false. Based on their answer, I want it to build it into a score board. And then post the results at the end of the quiz.
I’ve tried adding a target but can’t seem to even get that to log. I have a check answer function where it moves to the next question but I also want that click event to tally up a score.
Code:
var startQuizBtn = document.getElementById("start-quiz");
// start quiz button with a fuction to trigger the quiz questions (showQuestons())
startQuizBtn.onclick = function() {
console.log('button click')
showQuestions(), setTime();
};
console.log(setTime);
var timeEl = document.querySelector(".time");
var resultsEl = document.getElementById("results");
var secondsLeft = 15;
// function that starts timer when you click start quiz
function setTime() {
// Sets interval in variable
var timerInterval = setInterval(function() {
secondsLeft--;
timeEl.textContent = secondsLeft;
if (secondsLeft === 0) {
// Stops execution of action at set interval
clearInterval(timerInterval);
// Calls function to create and append results of quiz
userScore();
}
}, 1000);
}
// the questions that will be asked during the quiz
var myQuestions = [{
question: "Javascript is an _______ language?",
answers: ["Object-Oriented", "Object-Based", "Procedural", "None of the above"],
},
{
question: "Which of the following keywords is used to define a variable in Javascript?",
answers: ["var", "bar", "jar", "bob"],
},
{
question: "Which of the following methods is used to access HTML elements using Javascript?",
answers: ["targetElement", "thatOneElement", "getElementById", "idElement"],
},
];
var questionIndex = 0
console.log(myQuestions[questionIndex])
console.log(myQuestions[questionIndex].question)
// main function to show quiz question in a sequential order after clicking start quiz
function showQuestions() {
startQuizBtn.style.display = "none";
var quizAnswers = document.getElementById("answers");
var questionTitle = myQuestions[questionIndex].question
var questionh3 = document.getElementById("questions")
questionh3.textContent = questionTitle
quizAnswers.textContent = ""
myQuestions[questionIndex].answers.forEach(function(q) {
// takes answers and turns them into buttons below questions
var ul = document.getElementById("answers");
var li = document.createElement("li");
var answerBtn = document.createElement("button");
li.appendChild(answerBtn);
ul.appendChild(li)
// button styling
answerBtn.textContent = q
answerBtn.style.padding = "10px"
li.style.listStyle = "none"
li.style.marginTop = "5px"
li.style.marginBottom = "5px"
quizAnswers.appendChild(answerBtn)
answerBtn.addEventListener("click", checkAnswer)
answerBtn.addEventListener("click", userScore)
console.log(q)
})
}
var scoreCounter = 0;
var answerIndex = 0;
function userScore(e) {
var selectedAnswer =
console.log(selectedAnswer)
var correct = myQuestions.answers
if (selectedAnswer === correct) {
scoreCounter = scoreCounter + 1;
}
}
function checkAnswer() {
questionIndex++
showQuestions()
}
<div class="wrapper">
<header>
<h1>Code Quiz Challenge</h1>
<p id="quiz-rules">Welcome to the Javascript fundamentals quiz! Answer the questions to test your Javascript knowledge. This quiz does have a time limit. If you answer the question incorrectly, you will be penalized with ten seconds off your timer. To begin the quiz,
select Start Quiz below. </p>
</header>
<div id=quiz-container>
<div id="start-button-container">
<button id="start-quiz">Start Quiz</button>
</div>
<h2 class="time">
</h1>
<h3 id="questions"></h3>
<ul id="answers"></ul>
<div id="score"></div>
</div>
