JSFiddle - React, Tailwind, and code Playground
by mickhavoc
JavaScript
//quiz time with objects!
//question object/children & methods
function Question(theQuestion, theChoices, theCorrectAnswer) {
this.question = theQuestion;
this.choices = theChoices;
this.correctAnswer = theCorrectAnswer;
this.userAnswer = "";
var newDate = new Date(),
QUIZ_CREATED_DATE = newDate.toLocaleDateString();
this.getQuizDate = function () {
return QUIZ_CREATED_DATE;
};
//console.log("Quiz created on: " + this.getQuizDate());
}
Question.prototype = {
constructor: Question,
getCorrectAnswer: function () {
return this.correctAnswer;
},
getUserAnswer: function () {
return this.userAnswer;
},
displayQuestion: function () {
var questionToDisplay = '<div class="question">' + this.question +
'</div><ul>';
choiceCounter = 0;
this.choices.forEach(function (eachChoice) {
questionToDisplay += '<li><input type="radio" name="choice" value="' + choiceCounter + '">' + eachChoice + '</li>';
choiceCounter++;
});
questionToDisplay += '</ul>';
//console.log(questionToDisplay);
}
};
//children questions of the main question object - two types: multi. choice and drag/drop
//first, lets build inherit prototype pattern
function inheritPrototype(childObject, parentObject) {
var copyOfParent = Object.create(parentObject.prototype);
copyOfParent.constructor = childObject;
childObject.prototype = copyOfParent;
}
function MultipleChoiceQuestion(theQuestion,theChoices,theCorrectAnswer) {
Question.call(this, theQuestion,theChoices,theCorrectAnswer);
}
inheritPrototype(MultipleChoiceQuestion, Question);
function DragDropQuestion(theQuestion,theChoices,theCorrectAnswer) {
Question.call(this, theQuestion,theChoices,theCorrectAnswer);
}
inheritPrototype(DragDropQuestion, Question);
DragDropQuestion.prototype = {
displayQuestion: function() {
var body = document.body;
...