Quiz Class
Class that represents an HTML Quiz. Provides methods for checking answers, generating a score and providing visual feedback.
See https://alpsquid.github.io/quizlib for usage
Constructor
Quiz
-
quizContainer
-
answers
Parameters:
-
quizContainer
StringID of the quiz container element.
-
answers
ArrayArray of correct answers using the input value. e.g. ['a', '7', ['a', 'b']]. Can use nested arrays for multi-answers such as checkbox questions
Example:
new Quiz('quiz-div', ['a', '7', ['c', 'd'], 'squids', ['a', 'b']]);
Item Index
Properties
- answers
- Classes deprecated
- Classes
- result
- unansweredQuestionText
Methods
checkAnswers
-
[flagUnanswered=true]
Checks quiz answers against provided answers. Calls clearHighlights for each question.
Parameters:
-
[flagUnanswered=true]
Boolean optionalWhether to ignore unanswered questions. If false, unanswered questions will not be flagged
Returns:
True or if flagUnanswered is true: True if all questions have been answered. Otherwise false and unanswered questions are highlighted.
clearHighlights
-
question
Clears highlighting for a question element (correct and incorrect classes), including unanswered question warnings and elements using the Classes.TEMP class
Parameters:
-
question
HTMLDocumentQuestion element to clear
highlightResults
-
[questionCallback]
Highlights correctly and incorrectly answered questions by:
- Adding the class 'quizlib-correct' to correctly answered question titles
- Adding the class 'quizlib-incorrect' to incorrectly answered question titles
Parameters:
-
[questionCallback]
Function optionalOptional callback for each question with the following arguments:
- Quiz: the quiz object
- Element: the question element
- Number: question number
- Boolean: true if correct, false if incorrect.
This allows you to further customise the handling of answered questions (and decouples the library from a specific HTML structure), for example highlighting the correct answer(s) on incorrect questions.
Use the Classes.TEMP ('quizlib-temp') class on added elements that you want removing when checkAnswers is called
Example:
// Method Call
quiz.highlightResults(handleAnswers);
// handleAnswers callback
function handleAnswers(quizObject, questionElement, questionNo, correctFlag) {
...
}
Properties
answers
Array
User defined answers taken from constructor
Classes
Object
deprecated
final
Enum containing classes used by QuizLib as follows:
- QUESTION: 'quizlib-question'
- used to identify a question element
- QUESTION_TITLE: 'quizlib-question-title'
- used to identify the question title element
- QUESTION_WARNING: 'quizlib-question-answers'
- used to identify the element containing question answers
- QUESTION_ANSWERS: 'quizlib-question-warning'
- used by the 'unanswered question warning' element. Removed by clearHighlights
- CORRECT: 'quizlib-correct'
- added to question titles to highlight correctly answered questions.
Use freely to take advantage of highlightResults and clearHighlights
- added to question titles to highlight correctly answered questions.
- INCORRECT: 'quizlib-incorrect'
- added to question titles to highlight incorrectly answered questions.
Use freely to take advantage of highlightResults and clearHighlights
- added to question titles to highlight incorrectly answered questions.
- TEMP: 'quizlib-temp'
- Add to any elements you want to be removed by clearHighlights (called by checkAnswers). For example, adding an element with the correct answer in your clearHighlights callback and have it be removed automatically
Default: See above
Classes
Object
final
Enum containing classes used by QuizLib as follows:
- QUESTION: 'quizlib-question'
- used to identify a question element
- QUESTION_TITLE: 'quizlib-question-title'
- used to identify the question title element
- QUESTION_WARNING: 'quizlib-question-answers'
- used to identify the element containing question answers
- QUESTION_ANSWERS: 'quizlib-question-warning'
- used by the 'unanswered question warning' element. Removed by clearHighlights
- CORRECT: 'quizlib-correct'
- added to question titles to highlight correctly answered questions. Use freely to take advantage of highlightResults and clearHighlights
- INCORRECT: 'quizlib-incorrect'
- added to question titles to highlight incorrectly answered questions. Use freely to take advantage of highlightResults and clearHighlights
- TEMP: 'quizlib-temp'
- Add to any elements you want to be removed by clearHighlights (called by checkAnswers). For example, adding an element with the correct answer in your clearHighlights callback and have it be removed automatically
Default: See above
unansweredQuestionText
String
Warning displayed on unanswered questions
Default: 'Unanswered Question!'