-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
The previous version quiz is fixed as per current display logic
For the issue : #536
- Loading branch information
Showing
2 changed files
with
62 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
const quizContainer = document.getElementById("quiz"); | ||
const resultsContainer = document.getElementById("results"); | ||
const submitButton = document.getElementById("submit"); | ||
|
||
|
||
function showResults() { | ||
// gather answer containers from our quiz | ||
const answerContainers = quizContainer.querySelectorAll(".answers"); | ||
answerContainers.forEach(e => e.style.color = "black"); | ||
|
||
// keep track of user's answers | ||
let numCorrect = 0; | ||
|
||
// for each question... | ||
myQuestions.forEach((currentQuestion, questionNumber) => { | ||
// find selected answer | ||
const answerContainer = answerContainers[questionNumber]; | ||
const selector = `input[name=question${questionNumber}]:checked`; | ||
const userAnswer = (answerContainer.querySelector(selector) || {}).value; | ||
|
||
// if answer is correct | ||
if (userAnswer === currentQuestion.correctAnswer) { | ||
// add to the number of correct answers | ||
numCorrect++; | ||
|
||
// color the answers green | ||
//answerContainers[questionNumber].style.color = "lightgreen"; | ||
} else { | ||
// if answer is wrong or blank | ||
// color the answers red | ||
answerContainers[questionNumber].style.color = "red"; | ||
} | ||
}); | ||
|
||
// show number of correct answers out of total | ||
resultsContainer.innerHTML = `${numCorrect} out of ${myQuestions.length}`; | ||
} | ||
|
||
|
||
submitButton.addEventListener("click", showResults); |